Hello everyone. 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: Here is my code from models and controller
Authors.php
<?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
<?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
<?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
<?php
use Phalcon\Mvc\View,
Phalcon\Mvc\Controller;
class AdminController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
$this->view->setVar('books', Books::find()->toArray());
}
}
In view I'm iterating thru these books array using volt but all I get is only data from the books table
Array
(
[0] => Array
(
[ID] => 1
[Name] => Javascript: The Good Parts
[YearPublished] => 2014-04-18
[Picture] => javascript-the-good-parts.jpg
)
...