Reputation: 349
I'm trying to setup a 3 tables relationship in phalcon, but it doesn't seem to work. Could anyone plese tell me what am I doing wrong? Here is a ERD of these tables: (ERD diagram) Here is my code from models and controller
Authors.php (Model)
<?php
class Authors extends \Phalcon\Mvc\Model
{
/**
*
* @var integer
*/
public $ID;
/**
*
* @var string
*/
public $Name;
/**
*
* @var string
*/
public $LastName;
/**
* Initialize method for model.
*/
public function initialize()
{
$this->setSource('Authors');
$this->hasMany('ID', 'Authorbook', 'AuthorID');
}
/**
* Independent Column Mapping.
*/
public function columnMap()
{
return array(
'ID' => 'ID',
'Name' => 'Name',
'LastName' => 'LastName'
);
}
Books.php(Model)
<?php
class Books extends \Phalcon\Mvc\Model
{
/**
*
* @var integer
*/
public $ID;
/**
*
* @var string
*/
public $Name;
/**
*
* @var string
*/
public $YearPublished;
/**
*
* @var string
*/
public $Picture;
/**
* Initialize method for model.
*/
public function initialize()
{
$this->setSource('Books');
$this->hasMany('ID', 'Authorbook', 'BookID');
}
/**
* Independent Column Mapping.
*/
public function columnMap()
{
return array(
'ID' => 'ID',
'Name' => 'Name',
'YearPublished' => 'YearPublished',
'Picture' => 'Picture'
);
}
}
}
AuthorBook.php (Model):
<?php
class Authorbook extends \Phalcon\Mvc\Model
{
/**
*
* @var integer
*/
public $ID;
/**
*
* @var integer
*/
public $AuthorID;
/**
*
* @var integer
*/
public $BookID;
/**
* Initialize method for model.
*/
public function initialize()
{
$this->setSource('AuthorBook');
$this->belongsTo('AuthorID', 'Authors', 'ID');
$this->belongsTo('BookID', 'Books', 'ID');
}
/**
* Independent Column Mapping.
*/
public function columnMap()
{
return array(
'ID' => 'ID',
'AuthorID' => 'AuthorID',
'BookID' => 'BookID'
);
}
}
AdminController.php
public function indexAction()
{
$this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
$this->view->setVar('books', Books::find()->toArray());
}
}
So my question is how can I get access to the author of a book? Because when I print out my books array in view only thing I get is :
Array
(
[0] => Array
(
[ID] => 1
[Name] => Javascript: The Good Parts
[YearPublished] => 2014-04-18
[Picture] => javascript-the-good-parts.jpg
)
...
Upvotes: 1
Views: 2076
Reputation: 78
I copied an example code on Phalcon site to test model relationship and ran into similar issue. I need to specific the namespace when set relationship. For example, in function initialize of Robots class
public function initialize()
{
$this->getSource('robots');
$this->hasMany('id', 'SomeNameSpace\RobotsParts', 'robots_id',array(
'alias' => 'robotsparts',
));
}
Upvotes: 0
Reputation: 1026
You should use the hasManyToMany for n-n relationship for the Authors and Books models, (if I'm not wrong, Author has many Books and Books has many Authors)
so you models should be like this: for Authors:
public function initialize(){
$this->setSource('Authors');
$this->hasManyToMany('ID', 'Authorbook', 'AuthorID', 'BookID', 'Books', 'ID', array(
'alias' => 'books'
));
}
same for the Books model, we have to use the hasManyToMany function:
public function initialize(){
$this->setSource('Books');
$this->hasManyToMany('ID', 'Authorbook', 'BookID', 'AuthorID', 'Author', 'ID', array(
'alias' => 'authors'
));
}
the AuthorBook model relations are correct.
Now you can get the author(s) of a book with a simple call to the alias that we have defined in the relations:
$book = Books::findFirst();
$bookAuthor = $book->authors->toArray()
that's it.
Upvotes: 3