开发者

Doctrine Fatal Error - Unknown relation alias

I am getting following error message:

Doctrine_Table_Exception: Unknown relation alias shoesTable in /home/public_html/projects/giftshoes/system/database/doctrine/Doctrine/Relation/Parser.php on line 237

I am using doctrine 1.2.2 with Codeigniter

My Code is below: (BaseShoes.php and Shoes.php is auto generated)

------------BaseShoes------------

<?php
// Connection Component Binding
Doctrine_Manager::getInstance()->bindComponent('Shoes', 'sadiqsof_giftshoes');

/**
 * BaseShoes
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @property integer $sku
 * @property string $name
 * @property string $keywords
 * @property string $description
 * @property string $manufacturer
 * @property float $sale_price
 * @property float $price
 * @property string $url
 * @property string $image
 * @property string $category
 * @property Doctrine_Collection $Viewes
 * 
 * @package    ##PACKAGE##
 * @subpackage ##SUBPACKAGE##
 * @author     ##NAME## <##EMAIL##>
 * @version    SVN: $Id: Builder.php 6820 2009-11-30 17:27:49Z jwage $
 */
abstract class BaseShoes extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('shoes');
        $this->hasColumn('sku', 'integer', 4, array(
             'type' => 'integer',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => false,
             'length' => '4',
             ));
        $this->hasColumn('name', 'string', 255, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '255',
             ));
        $this->hasColumn('keywords', 'string', 255, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '255',
             ));
        $this->hasColumn('description', 'string', null, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '',
             ));
        $this->hasColumn('manufacturer', 'string', 20, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '20',
             ));
        $this->hasColumn('sale_price', '开发者_运维知识库float', null, array(
             'type' => 'float',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '',
             ));
        $this->hasColumn('price', 'float', null, array(
             'type' => 'float',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '',
             ));
        $this->hasColumn('url', 'string', null, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '',
             ));
        $this->hasColumn('image', 'string', null, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '',
             ));
        $this->hasColumn('category', 'string', 50, array(
             'type' => 'string',
             'fixed' => 0,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             'length' => '50',
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasMany('Viewes', array(
             'local' => 'sku',
             'foreign' => 'sku'));
    }
}

--------------ShoesTable--------

<?php
class ShoesTable extends Doctrine_Table
{
    function getAllShoes($from = 0, $total = 15)
    {
        $q = Doctrine_Query::create()
        ->from('Shoes')
        ->limit($total)
        ->offset($from);

        return $q->execute(array(), Doctrine::HYDRATE_ARRAY);
    }

}

---------------Shoes Model-----------------

<?php

/**
 * Shoes
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    ##PACKAGE##
 * @subpackage ##SUBPACKAGE##
 * @author     ##NAME## <##EMAIL##>
 * @version    SVN: $Id: Builder.php 6820 2009-11-30 17:27:49Z jwage $
 */
class Shoes extends BaseShoes
{
    function  __construct() {
        parent::__construct();
        $this->shoesTable = Doctrine::getTable('Shoes');
    }

    function getAllShoes()
    {
        return $this->shoesTable->getAllShoes();
    }

}


For anyone else searching for answers, I ran into this error message because I had

Doctrine_Query::create()->from('Subscription s')->innerJoin('BillingPlan b')

Instead of

Doctrine_Query::create()->from('Subscription s')->innerJoin('s.BillingPlan b')

The relation needs to be relative to the model listed in from()


Not sure why you get this specific error, but, judging from Doctrine's manual, you should not define the __construct() method in your Shoes class (that extends BaseShoes, which, itself, extends Doctrine_Record)

Here's the section of Doctrine's manual that says just this : Overriding the Constructor (quoting) :

Doctrine doesn't allow you to override the Doctrine_Record::__construct() method but provides an alternative
[...]

(I didn't copy-paste everything : there is more interesting stuff there ;-) )


You haven't defined $shoesTable as a property on the Shoes object. Add it like this:

class Shoes extends BaseShoes
{
    private $shoesTable;

    function  __construct() {
        parent::__construct();
        $this->shoesTable = Doctrine::getTable('Shoes');
    }

    function getAllShoes()
    {
        return $this->shoesTable->getAllShoes();
    }

}

If you don't, it doesn't exist, and referencing it will call the __get() magic method which Doctrine provides. Doctrine will assume that shoesTable is part of the data model, and look for a relation (since it can't find any local data) with that name.

Another, easier way of doing it is to use the built in getTable() method:

class Shoes extends BaseShoes
{

    function getAllShoes()
    {
        return $this->getTable()->getAllShoes();
    }

}


Removing the constructor without doing something with the code you've removed probably won't help.

I am new to Doctrine, but I'd had the same error recently (I think for different reasons). I wonder if the class attribute $this->shoesTable is taken to mean a foreign relation, which would explain the error message. In any case, you probably don't want to have a table method in a row class, so I would be inclined to do this:

Doctrine_Core::getTable('Shoes')->getAllShoes();

I know it's unlikely you're still awaiting this, but if you want to continue the dialogue, post your calling code.


I've faced with the same issue (on Symfony 1.4) and after all the checks done (I've started the Jobeet tutorial) there was no differences.

First, you must check the names of the class and aliases (it is case sensitive)! If all are good, please check the schema.yml there in relations part you must add the foreign column, but also the foreignAlias. That I forgot.

I hope it helps.

JobeetJob:
  connection: doctrine
  tableName: jobeet_job
  ...
  relations:
    JobeetCategory:
      local: category_id
      foreign: id
      foreignAlias: JobeetJobs
      type: one
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜