dongzhizhai4070
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
    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...

    点赞 评论

相关推荐