Phalcon php orm无法关联表
Posted
技术标签:
【中文标题】Phalcon php orm无法关联表【英文标题】:Phalcon php orm can't relate tables 【发布时间】: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
)
...
【问题讨论】:
【参考方案1】:对于 Authors 和 Books 模型,您应该使用 hasManyToMany for n-n 关系,(如果我没记错的话,Author has many Books and Books has many Authors)
所以你的模型应该是这样的: 作者:
public function initialize()
$this->setSource('Authors');
$this->hasManyToMany('ID', 'Authorbook', 'AuthorID', 'BookID', 'Books', 'ID', array(
'alias' => 'books'
));
对于 Books 模型也是如此,我们必须使用 hasManyToMany 函数:
public function initialize()
$this->setSource('Books');
$this->hasManyToMany('ID', 'Authorbook', 'BookID', 'AuthorID', 'Author', 'ID', array(
'alias' => 'authors'
));
AuthorBook 模型关系正确。
现在您可以通过简单调用我们在关系中定义的别名来获取一本书的作者:
$book = Books::findFirst();
$bookAuthor = $book->authors->toArray()
就是这样。
【讨论】:
【参考方案2】:我复制了一个example code on Phalcon site 来测试模型关系并遇到了类似的问题。我需要在设置关系时指定命名空间。例如,在 Robots 类的函数初始化中
public function initialize()
$this->getSource('robots');
$this->hasMany('id', 'SomeNameSpace\RobotsParts', 'robots_id',array(
'alias' => 'robotsparts',
));
【讨论】:
以上是关于Phalcon php orm无法关联表的主要内容,如果未能解决你的问题,请参考以下文章
除了Phalcon php中的indexAction之外,无法调用indexController的动作