Doctrine PHP关系出错 - >未找到列:1054'on子句'中的未知列'e.id'

时间:2012-02-06 15:06:59

标签: php orm doctrine doctrine-1.2

我有这两个模型

{
    class Email extends Doctrine_Record
    {
        public function setTableDefinition()
        {
            $this->setTableName('email');
            $this->hasColumn('id', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => true,
                 'autoincrement' => true,
                 ));
            $this->hasColumn('nombre', 'string', 40, array(
                 'type' => 'string',
                 'length' => 40,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('email', 'string', 90, array(
                 'type' => 'string',
                 'length' => 90,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('estado', 'integer', 1, array(
                 'type' => 'integer',
                 'length' => 1,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'default' => '1',
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('borrado', 'integer', 1, array(
                 'type' => 'integer',
                 'length' => 1,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'default' => '0',
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('created_at', 'timestamp', null, array(
                 'type' => 'timestamp',
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('updated_at', 'timestamp', null, array(
                 'type' => 'timestamp',
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
        }

        public function setUp()
        {
            parent::setUp();

            $this->hasMany('Grupo as Grupos', array(
                    'refClass' => 'EmailGrupo',
                    'local' => 'email_id',
                    'foreign' => 'grupo_id'));

            $this->hasMany('Envio as Envios', array(
                    'local' => 'email_id',
                    'foreign' => 'envio_id',
                    'refClass' => 'EnvioEmail'));
        }

        //setters   
        public function setId($id) { $this->_set('id', $id); }
        public function setNombre($nombre) { $this->_set('nombre', $nombre); }
        public function setEmail($email) { $this->_set('email', $email); }
        public function setEstado($estado) { $this->_set('estado', $estado); }
        public function setBorrado($borrado) { $this->_set('borrado', $borrado); }

        //getters
        public function getId() { return $this->_get('id'); }
        public function getNombre() { return $this->_get('nombre'); }
        public function getEmail() { return $this->_get('email'); }
        public function getEstado() { return $this->_get('estado'); }
        public function getBorrado() { return $this->_get('borrado'); }
        public function getGrupos() { return $this->_get('Grupos'); }
        public function getEnvios() { return $this->_get('Envios'); }

        public function delete(){
            $this->setBorrado(1);
            $this->save();
        }


    }

}

{

    class EmailGrupo extends Doctrine_Record
    {
        public function setTableDefinition()
        {
            $this->setTableName('email_grupo');
            $this->hasColumn('email_id', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('grupo_id', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => true,
                 'autoincrement' => false,
                 ));
        }

        public function setUp()
        {
            parent::setUp();

            $this->hasOne('Email', array(
                 'local' => 'email_id',
                 'foreign' => 'id'));

            $this->hasOne('Grupo', array(
                 'local' => 'grupo_id',
                 'foreign' => 'id'));


        }

        // setters
        public function setEmailId($email_id) { $this->_set('email_id', $email_id); }
        public function setGrupoId($grupo_id) { $this->_set('grupo_id', $grupo_id); }


        // getters
        public function getEmailId() { return $this->_get('email_id'); }
        public function getGrupoId() { return $this->_get('grupo_id'); }
    }

}

当我尝试让“Grupos”执行$email->getGrupos();时,我收到此错误:

  

致命错误:未捕获的异常'Doctrine_Connection_Mysql_Exception'   消息'SQLSTATE [42S22]:找不到列:1054未知   F:\ Proyectos \ Flash中'on子句''中的列'e.id'   Moda \ code \ application \ helpers \ doctrine \ lib \ Doctrine \ Connection.php on   第1083行。

我记录了查询,我得到了:

  

SELECT g.id AS g_ id,g.nombre AS g _nombre,g.borrado AS g__borrado,   g.created_at AS g__created_at,g.updated_at AS g__updated_at,   e.email_id AS e__email_id,egrupo_id AS e__grupo_id FROM grupo g LEFT   JOIN email_grupo e ON g.id = e.id WHERE(e.email_id IN(?))

为什么加入的右侧是错误的?感谢

更新:添加grupo模型以及我如何获得$ email

class Grupo extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('grupo');
        $this->hasColumn('id', 'integer', 4, array(
             'type' => 'integer',
             'length' => 4,
             'fixed' => false,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => true,
             ));
        $this->hasColumn('nombre', 'string', 40, array(
             'type' => 'string',
             'length' => 40,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             ));
        $this->hasColumn('borrado', 'integer', 1, array(
             'type' => 'integer',
             'length' => 1,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'default' => '0',
             'notnull' => true,
             'autoincrement' => false,
             ));
        $this->hasColumn('created_at', 'timestamp', null, array(
             'type' => 'timestamp',
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             ));
        $this->hasColumn('updated_at', 'timestamp', null, array(
             'type' => 'timestamp',
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             ));
    }

    public function setUp()
    {
        parent::setUp();

        $this->actAs('Timestampable');      

       $this->hasMany('Email as Emails', array(
                'local' => 'id',
                'foreign' => 'grupo_id',
                'refClass' => 'EmailGrupo'
            )
         );

         $this->hasMany('Envio as Envios', array(
                'local' => 'id',
                'foreign' => 'grupo_id',
                'refClass' => 'EnvioGrupo'
            )
         ); 
    }

    //setters
    public function setId($id) { $this->_set('id', $id); }
    public function setNombre($nombre) { $this->_set('nombre', $nombre); }
    public function setBorrado($borrado) { $this->_set('borrado', $borrado); }

    // getters
    public function getId() { return $this->_get('id'); }
    public function getNombre() { return $this->_get('nombre'); }
    public function getBorrado() { return $this->_get('borrado'); }

    public function delete(){
        $this->setBorrado(1);
        $this->save();
    }

}

$email = Doctrine::getTable('Email')->find($this->uri->segment(4,0));

1 个答案:

答案 0 :(得分:0)

错误发生在grupo模型中,这是正确的模型:

class Grupo extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('grupo');
        $this->hasColumn('id', 'integer', 4, array(
             'type' => 'integer',
             'length' => 4,
             'fixed' => false,
             'unsigned' => false,
             'primary' => true,
             'autoincrement' => true,
             ));
        $this->hasColumn('nombre', 'string', 40, array(
             'type' => 'string',
             'length' => 40,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             ));
        $this->hasColumn('borrado', 'integer', 1, array(
             'type' => 'integer',
             'length' => 1,
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'default' => '0',
             'notnull' => true,
             'autoincrement' => false,
             ));
        $this->hasColumn('created_at', 'timestamp', null, array(
             'type' => 'timestamp',
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             ));
        $this->hasColumn('updated_at', 'timestamp', null, array(
             'type' => 'timestamp',
             'fixed' => false,
             'unsigned' => false,
             'primary' => false,
             'notnull' => true,
             'autoincrement' => false,
             ));
    }

    public function setUp()
    {
        parent::setUp();

        $this->actAs('Timestampable');      

        $this->hasMany('Email as Emails', array(
                'local' => 'grupo_id',
                'foreign' => 'email_id',
                'refClass' => 'EmailGrupo'
            )
         );

        $this->hasMany('Envio as Envios', array(
                'local' => 'grupo_id',
                'foreign' => 'envio_id',
                'refClass' => 'EnvioGrupo'
            )
         ); 
    }

    //setters
    public function setId($id) { $this->_set('id', $id); }
    public function setNombre($nombre) { $this->_set('nombre', $nombre); }
    public function setBorrado($borrado) { $this->_set('borrado', $borrado); }

    // getters
    public function getId() { return $this->_get('id'); }
    public function getNombre() { return $this->_get('nombre'); }
    public function getBorrado() { return $this->_get('borrado'); }


}

我用电子邮件修改了定义重新图标的行。