【发布时间】:2014-04-20 10:10:32
【问题描述】:
我正在尝试在 phalcon 中设置 3 个表的关系,但它似乎不起作用。谁能告诉我我做错了什么?这是这些表的 ERD:(ERD diagram) 这是我的模型和控制器代码
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(模型)
<?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
public function indexAction()
{
$this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
$this->view->setVar('books', Books::find()->toArray());
}
}
所以我的问题是我怎样才能接触到一本书的作者?因为当我在视图中打印出我的书籍数组时,我得到的只是:
Array
(
[0] => Array
(
[ID] => 1
[Name] => Javascript: The Good Parts
[YearPublished] => 2014-04-18
[Picture] => javascript-the-good-parts.jpg
)
...
【问题讨论】:
标签: php orm model entity-relationship phalcon