dongzhizhai4070 2012-05-10 13:58 采纳率: 0%
浏览 67
已采纳

Zend Framework&Doctrine 2 findBy使用实体返回空数组

I've been struggling with some findBy using an entity and I really can't understand what's wrong.

I'm using these two classes:

GPos_Model_Product :

/**
* @Entity
* @Table(name="Product")
*/
class GPos_Model_Product extends GPos_Doctrine_ActiveEntity {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

    /**
     * @ManyToOne(targetEntity="GPos_Model_Store")
     * @JoinColumn(name="store_id", referencedColumnName="id")
     **/
    protected $store;
}

GPos_Model_Store:

/**
 * @Entity
 * @Table(name="Store")
 */
class GPos_Model_Store extends GPos_Doctrine_ActiveEntity {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

    /** @Column(name="status", type="string", columnDefinition="enum('active', 'deleted')")  */
    protected $status = 'active';
}

Note: I've removed useless fields in both class to make it more readable

So here's the problem: In one of my controller I'm trying to retrieve all products that are linked to a certain store:

public function indexAction() {
    $this->_helper->getHelper('layout')->disableLayout();
    $authNamespace = new Zend_Session_Namespace('Zend_Auth');

    //get store's products list.
    $store = GPos_Model_Store::find($authNamespace->store);
    var_dump($store); //prints store successfully.
    //next line throws an unusable exception talking about layout.phtml not found...
    $products = GPos_Model_Product::findByStore($store->getId());
    //give it to the view for the products list rendering.
    var_dump($products);
    $this->view->products = $products;
}

Weird enough, when I use $products = GPos_Model_Product::findByStore($store); instead, I get no exception but simply an empty array as a result. I used the exact same way (with getId()) in another controller about two other entities that have the same relation and that worked fine.

I checked my DB and the store I'm using as a parameter is indeed bound to a few products which means an empty array as a result isn't correct either. I should retrieve an array of like 8 products...

Here's the code of my other controller that is working fine (narrowed the code again):

$user = GPos_Model_User::findOneByLogin($form->getValue('login'));
$contact = GPos_Model_Contact::findByUser($user->getId());
//these lines work perfectly and I'm receiving an array of `GPos_Model_Contact` entities... 

These two pairs of entities are declared in the exact same way so I really don't get it...

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • douba3943 2012-05-11 10:11
    关注

    YEAH!

    I found my mistake... It actually had nothing to do with my relations.
    My mistake was that I use gedmo and forgot to set a default locale, meaning it just couldn't retrieve any products because it didn't know where to look. Gedmo was throwing an exception but it was overridden by the layout not found exception (which is kind of a zend bug because there was no problem with my layout).

    Thanks to XDebug and a few hours of patience I was finally able to find the exact exception message and fix it in a few minutes.

    Thanks for anyone who paid attention anyway, it was my fault not providing enough code (with the gedmo stuff) because I thought this was actually well set...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料