/** * Reset Database content to sample content */ public static function resetDatabase() { $connection = ConnectionManager::getConnection(); // Clear Relation cache RelationManager::clearCache(); $connection->exec("TRUNCATE TABLE post;"); $connection->exec("TRUNCATE TABLE category;"); $connection->exec("TRUNCATE TABLE author;"); $connection->exec("TRUNCATE TABLE postchange;"); $connection->exec("TRUNCATE TABLE student;"); $connection->exec("TRUNCATE TABLE course;"); $connection->exec("TRUNCATE TABLE student_courses;"); // Fill the test data $category = array(array('id' => '1', 'name' => 'News', 'description' => 'Site news.'), array('id' => '2', 'name' => 'Press', 'description' => 'Press news'), array('id' => '3', 'name' => 'FAQ', 'description' => 'FAQ Posts'), array('id' => '4', 'name' => 'Downloads', 'description' => 'Download Posts')); $authors = array(array('id' => '1', 'name' => 'First Author', 'email' => null), array('id' => '2', 'name' => 'Jan the Author', 'email' => '*****@*****.**'), array('id' => '3', 'name' => 'Eric Authorinus', 'email' => '*****@*****.**')); $posts = array(array('id' => '1', 'authorid' => '1', 'categoryid' => '1', 'title' => 'Sample News #1', 'content' => 'Sample News 1'), array('id' => '2', 'authorid' => '1', 'categoryid' => '1', 'title' => 'Sample News #2', 'content' => 'Sample News 2'), array('id' => '3', 'authorid' => '1', 'categoryid' => '1', 'title' => 'Sample News #3', 'content' => 'Sample News 3'), array('id' => '4', 'authorid' => '1', 'categoryid' => '1', 'title' => 'Sample News #4', 'content' => 'Sample News 4'), array('id' => '5', 'authorid' => '2', 'categoryid' => '2', 'title' => 'Sample Press #1', 'content' => 'Sample Press 1'), array('id' => '6', 'authorid' => '2', 'categoryid' => '2', 'title' => 'Sample Press #2', 'content' => 'Sample Press 2'), array('id' => '7', 'authorid' => '2', 'categoryid' => '3', 'title' => 'Sample FAQ #1', 'content' => 'Sample FAQ 1'), array('id' => '8', 'authorid' => '3', 'categoryid' => '3', 'title' => 'Sample FAQ #2', 'content' => 'Sample FAQ 2'), array('id' => '9', 'authorid' => '3', 'categoryid' => '4', 'title' => 'Sample Downloads #1', 'content' => 'Sample Downloads 1'), array('id' => '10', 'authorid' => '3', 'categoryid' => '4', 'title' => 'Sample News #2', 'content' => 'Sample Downloads 2')); $postchanges = array(array('id' => '1', 'postid' => '1', 'authorid' => '1', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '2', 'postid' => '1', 'authorid' => '2', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '3', 'postid' => '1', 'authorid' => '2', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '4', 'postid' => '2', 'authorid' => '2', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '5', 'postid' => '2', 'authorid' => '3', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '6', 'postid' => '2', 'authorid' => '1', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '7', 'postid' => '3', 'authorid' => '1', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '8', 'postid' => '4', 'authorid' => '1', 'created' => date('c', strtotime('17-01-2016 00:00'))), array('id' => '9', 'postid' => '4', 'authorid' => '3', 'created' => date('c', strtotime('17-01-2016 00:00')))); $courses = array(array('id' => '1', 'name' => 'Course #1', 'description' => null), array('id' => '2', 'name' => 'Course #2', 'description' => 'Optional Description')); $students = array(array('id' => '1', 'name' => 'Student #1'), array('id' => '2', 'name' => 'Student #2'), array('id' => '3', 'name' => 'Student #3'), array('id' => '4', 'name' => 'Student #4'), array('id' => '5', 'name' => 'Student #5'), array('id' => '6', 'name' => 'Student #6'), array('id' => '7', 'name' => 'Student #7'), array('id' => '8', 'name' => 'Student #8'), array('id' => '9', 'name' => 'Student #9'), array('id' => '10', 'name' => 'Student #10')); $student_courses = array(array('student_id' => 1, 'course_id' => 1), array('student_id' => 1, 'course_id' => 2), array('student_id' => 2, 'course_id' => 1), array('student_id' => 4, 'course_id' => 1), array('student_id' => 4, 'course_id' => 2), array('student_id' => 5, 'course_id' => 2), array('student_id' => 6, 'course_id' => 1), array('student_id' => 7, 'course_id' => 2), array('student_id' => 8, 'course_id' => 1), array('student_id' => 9, 'course_id' => 1), array('student_id' => 9, 'course_id' => 2), array('student_id' => 10, 'course_id' => 1), array('student_id' => 10, 'course_id' => 2)); foreach ($category as $row) { $sql = "INSERT INTO category (id, name, description) VALUES (?, ?, ?);"; $query = $connection->prepare($sql); $idx = 1; foreach ($row as $column => $value) { $query->bindValue($idx, $value); $idx++; } $query->execute(); } foreach ($authors as $row) { $sql = "INSERT INTO author (id, name, email) VALUES (?, ?, ?);"; $query = $connection->prepare($sql); $idx = 1; foreach ($row as $column => $value) { $query->bindValue($idx, $value); $idx++; } $query->execute(); } foreach ($posts as $row) { $sql = "INSERT INTO post (id, authorid, categoryid, title, content) VALUES (?, ?, ?, ?, ?);"; $query = $connection->prepare($sql); $idx = 1; foreach ($row as $column => $value) { $query->bindValue($idx, $value); $idx++; } $query->execute(); } foreach ($postchanges as $row) { $sql = "INSERT INTO postchange (id, postid, authorid, created) VALUES (?, ?, ?, ?);"; $query = $connection->prepare($sql); $idx = 1; foreach ($row as $column => $value) { $query->bindValue($idx, $value); $idx++; } $query->execute(); } foreach ($courses as $row) { $sql = "INSERT INTO course (id, name, description) VALUES (?, ?, ?);"; $query = $connection->prepare($sql); $idx = 1; foreach ($row as $column => $value) { $query->bindValue($idx, $value); $idx++; } $query->execute(); } foreach ($students as $row) { $sql = "INSERT INTO student (id, name) VALUES (?, ?);"; $query = $connection->prepare($sql); $idx = 1; foreach ($row as $column => $value) { $query->bindValue($idx, $value); $idx++; } $query->execute(); } foreach ($student_courses as $row) { $sql = "INSERT INTO student_courses (student_id, course_id) VALUES (?, ?);"; $query = $connection->prepare($sql); $idx = 1; foreach ($row as $column => $value) { $query->bindValue($idx, $value); $idx++; } $query->execute(); } }
/** * @covers \SweetORM\Entity * @covers \SweetORM\EntityManager * @covers \SweetORM\Structure\RelationManager * @covers \SweetORM\Structure\RelationManager::saveRelations * @covers \SweetORM\Database\Query * @covers \SweetORM\Database\QueryGenerator * @covers \SweetORM\Database\Solver * @covers \SweetORM\Database\Solver\ManyToMany * @covers \SweetORM\Database\Solver\ManyToMany::solveSave */ public function testSaveManyToMany() { Utilities::resetDatabase(); // We will get student 2 first, the student already got course with id 1! $student = Student::get(2); /** @var Student $student */ $this->assertEquals(2, $student->_id); // Add course with id 2 $student->courses[] = Course::get(2); // Check if it's in the array $this->assertCount(2, $student->courses); // Save the student, this should solve the relation updates too $result = $student->save(); $this->assertTrue($result); // Verify if it worked by clearing caches and re-fetch the student and courses RelationManager::clearCache(); $student = Student::get(2); $this->assertEquals(2, $student->_id); $this->assertCount(2, $student->courses); // Check the other way around $course = Course::get(2); /** @var Course $course */ $found = false; foreach ($course->students as $student) { if ($student->_id == 2) { $found = true; } } $this->assertTrue($found); // Test deleting all and inserting none RelationManager::clearCache(); $student = Student::get(2); $student->courses->clear(); $student->save(); RelationManager::clearCache(); $student = Student::get(2); $this->assertCount(0, $student->courses); // Testing replacing all for one. RelationManager::clearCache(); $student = Student::get(2); $student->courses->clear(); $student->courses[] = Course::get(2); $student->save(); RelationManager::clearCache(); $student = Student::get(2); $this->assertCount(1, $student->courses); }
/** * Clear Lazy Fetching cache, will force to reload relationship entities. * You must call this when you expect new data from the database! */ public static function clearCache() { RelationManager::clearCache(); }
/** * Save Entity (will insert or update) * * @param Entity $entity * * @return bool status of save */ public function save($entity) { $query = new Query($entity, false); $structure = $this->getEntityStructure($entity); if ($entity->_saved) { // Update $result = $query->update()->set($this->getEntityDataArray($entity))->where(array($structure->primaryColumn->name => $entity->_id))->apply(); // Update relations RelationManager::with($entity)->saveRelations(); return $result; } else { // Insert $id = $query->insert()->into($structure->tableName)->values($this->getEntityDataArray($entity))->apply(); if ($id === false) { return false; // @codeCoverageIgnore } // Update relations RelationManager::with($entity)->saveRelations(); // Save ID and state $entity->{$structure->primaryColumn->propertyName} = $id; $entity->_id = $id; $entity->_saved = true; return true; } }