Tables relationships

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: http://tinypic.com/view.php?pic=k4h4ee&s=8#.U1D%56Pl%5s1M 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
        )
        ...


26.0k

Hello. What data you want to get?



18.5k
edited Apr '14

How can I access the book's author name after I call Books::find()->toArray() in a controller? How can I write it in view



26.0k

Maybe something like:

    //first change the model Books

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource('Books');
        $this->hasMany('ID', 'Authorbook', 'BookID', array('alias' => 'authors'));
    } 

    //test in Controller
    $books = Books::find(); //All books
    foreach ($books as $book) {
        print_r($book->getAuthors()->toArray());
    }


18.5k

Didn't work. It seems to me that those Models aren't getting connected somehow. But everything looks correct to me