Phalcon php orm无法关联表格

时间:2014-04-20 10:10:33

标签: php orm model entity-relationship phalcon

我正在尝试在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
)
...

2 个答案:

答案 0 :(得分:3)

你应该使用hasManyToMany为作者和书籍模型的n-n关系,(如果我没有错,作者有很多书籍和书籍有很多作者)

所以你的模型应该是这样的: 作者:

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()

是的。

答案 1 :(得分:0)

我复制了一个example code on Phalcon site来测试模型关系并遇到了类似的问题。我需要在设置关系时具体说明命名空间。例如,在函数初始化机器人类

public function initialize() { $this->getSource('robots'); $this->hasMany('id', 'SomeNameSpace\RobotsParts', 'robots_id',array( 'alias' => 'robotsparts', )); }