public function testRelationJoin() { $this->initConnection(); $select = Authors::select(); $select->relationJoin('warehouse', array(), array('warehouse.amount', 'bookName' => 'books.name')); $result = $select->fetchAll(); $result = $this->inline($result); $expected = "array(0=>array('author_id'=>'1','first_name'=>'Matthew','last_name'=>'Normani','birth_date'=>'1982-06-06','amount'=>'2','bookName'=>'LearningPHP,MySQL&JavaScript:WithjQuery,CSS&HTML5',),1=>array('author_id'=>'1','first_name'=>'Matthew','last_name'=>'Normani','birth_date'=>'1982-06-06','amount'=>'10','bookName'=>'We\\'reAllDamaged',),2=>array('author_id'=>'1','first_name'=>'Matthew','last_name'=>'Normani','birth_date'=>'1982-06-06','amount'=>'11','bookName'=>'JavaScriptandJQuery:InteractiveFront-EndWebDevelopment',),3=>array('author_id'=>'2','first_name'=>'Roji','last_name'=>'Normani','birth_date'=>'1977-03-15','amount'=>'10','bookName'=>'We\\'reAllDamaged',),4=>array('author_id'=>'3','first_name'=>'Anna','last_name'=>'Kowalska','birth_date'=>'1999-05-15','amount'=>'11','bookName'=>'JavaScriptandJQuery:InteractiveFront-EndWebDevelopment',),)"; $this->assertEquals($expected, $result); // not clear relation $select = AuthorsAddresses::select(); $select->relationJoin('$authors_addresses_type:dictionary_values', array('dictionary_values' => 'dtype'), array('type' => 'dtype.value')); $select->relationJoin('$authors_addresses_country:dictionary_values', array('dictionary_values' => 'dcountry'), array('country' => 'dcountry.value')); $select->relationJoin('$authors_addresses_street_prefix:dictionary_values', array('dictionary_values' => 'dprefix'), array('street_prefix' => 'dprefix.value')); $result = $select->fetchAll(); $result = $this->inline($result); $expected = "array(0=>array('author_id'=>'1','type'=>'correspondence','street_prefix'=>'Road','street'=>'1ChapelHill','country'=>'Poland',),1=>array('author_id'=>'2','type'=>'correspondence','street_prefix'=>'Road','street'=>'56/45ApelMill','country'=>'Poland',),2=>array('author_id'=>'3','type'=>'business','street_prefix'=>'Road','street'=>'45/23Apply','country'=>'USA',),)"; $this->assertEquals($expected, $result); }
// Collection $authors = Authors::select()->order('authors.author_id', 'DESC')->all(); foreach ($authors as $author) { $authorNew->set('first_name', 'John'); $authors->save(); } // or short $authors->set('first_name', 'John')->save(); // or delete all authors // $authors->delete(); // joins $select = Authors::select(); // you can only give column name to join $select->innerJoin('books_authors', "author_id"); // automaticly add column during join $select->leftJoin('books', "book_id", array('bookName' => 'books.name')); // joins with table alias, plus condition as list of column $select->rightJoin(array('o' => 'books_opinions'), array("book_id"), array('o.opinion', 'authorOfOpinion' => 'o.author')); // add column $select->column('o.book_id', 'opinionBookId'); // set order $select->order('authors.first_name'); $result = $select->fetchAll(); // relations joins $select = Authors::select(); $select->relationJoin('books_opinions'); $author1 = Authors::select()->equal('author_id', 1)->first(); // implement this method //$opinions = $author1->opinionsOfBook(1); //$collection = $opinions->all(); //$firstOpinion = $opinions->first();
public function testRelation() { $this->initConnection(); $authors = Authors::select()->equal('author_id', 1)->first(); $select = $authors->relation("books"); $result = $select->fetchAll(); $result = $this->inline($result); $expected = "array(0=>array('book_id'=>'1','name'=>'LearningPHP,MySQL&JavaScript:WithjQuery,CSS&HTML5','release_date'=>NULL,'format_id'=>NULL,),1=>array('book_id'=>'2','name'=>'We\\'reAllDamaged','release_date'=>NULL,'format_id'=>NULL,),2=>array('book_id'=>'3','name'=>'JavaScriptandJQuery:InteractiveFront-EndWebDevelopment','release_date'=>'2016-06-06','format_id'=>NULL,),)"; $this->assertEquals($expected, $result); $authors = Authors::select()->equal('author_id', 1)->first(); $select = $authors->relation("warehouse"); $result = $select->fetchAll(); $result = $this->inline($result); $expected = "array(0=>array('book_id'=>'1','amount'=>'2',),1=>array('book_id'=>'2','amount'=>'10',),2=>array('book_id'=>'3','amount'=>'11',),)"; $this->assertEquals($expected, $result); $authors = Authors::select()->equal('author_id', 1)->first(); $select = $authors->relation("warehouse"); $result = $select->fetchAll(); $result = $this->inline($result); $expected = "array(0=>array('book_id'=>'1','amount'=>'2',),1=>array('book_id'=>'2','amount'=>'10',),2=>array('book_id'=>'3','amount'=>'11',),)"; $this->assertEquals($expected, $result); $warehouse = Warehouse::select()->equal('book_id', 1)->first(); $select = $authors->relation("warehouse"); $result = $select->fetchAll(); $result = $this->inline($result); $expected = "array(0=>array('book_id'=>'1','amount'=>'2',),1=>array('book_id'=>'2','amount'=>'10',),2=>array('book_id'=>'3','amount'=>'11',),)"; $this->assertEquals($expected, $result); }