2015-06-30 94 views
0

我一直在努力解決這個問題,但沒有運氣。學說一對多/多對一。不工作

一是一些澄清:

基本上我要創建「用戶」和「存儲」,我的計劃,以確定您的收藏夾商店特定的用戶,並且還從存儲的角度看,誰在一個商店之間的關係特別的商店作爲他們喜歡的商店 我的猜測是使用ManyToMany關係,但在查找關於此的示例後,許多網站描述如果您需要與其他字段的ManyToMany關係(在我的情況下,我有3個額外的字段),您必須使用OneToMany/ManyToOne而不是

我使用SQLite我DB只是爲了發展,我寫的用超薄框架得到使用這樣的特定的實體時

一切正常,在PHP REST API。

/** 
    * Fetching user by email 
    * @param String $email User email 
    */ 
    public function getUserByEmail($email) { 
    $user = $this->getEntityManager()->getRepository('User') 
            ->findOneBy(array('email' => $email)); 
    if ($user === null) { 
     return null; 
    } else { 
     return $user; 
    } 
} 

$ProductStore = $this->getEntityManager()->getRepository('ProductStore') 
         ->findOneBy(array('product' => $product, 
             'store' => $store)); 

$Store = $this->getEntityManager()->getRepository('Store') 
         ->findOneBy(array('state' => $State->getId(), 
             'city' => $City->getId(), 
             'streetname' => $streetname, 
             'streetnumber' => $streetnumber)); 

//Store has many ManyToOne relations to other Entities 
//like State or City, but all of them works as expected 

一旦我得到我能讀懂他們的屬性的任何實體。除了當我嘗試獲取:

echo $User->getShopsAt(); i get an error saying "Object of class Doctrine\ORM\PersistentCollection could not be converted to string" 

,如果我嘗試

echo [$User->getShopsAt()]; i get "Array" as reponse 

,如果我嘗試

echo count([$User->getShopsAt()]); i get 1 

如果我理解調用getShopsAt()應返回的所有實例「UserStore」實體匹配用戶ID,但我無法訪問其任何屬性。 我試過的一切都失敗了,另外我想指出的是,我使用了UserStore表中有兩個條目的用戶ID「1」。那麼爲什麼計數返回1.

我已經創建了使用YML格式的3架構,因爲我覺得它比PHP更清晰。

有了這些模式我創建使用命令我的實體「供應商/斌/學說的ORM:生成實體./src」 (見底部產生的實體)

然後,我創建數據庫模式使用供應商/斌/學說ORM:架構工具:更新--force (參見SQL數據庫,並在底部數據)

這個我已經創建了使用Advaced REST客戶端擴展數據庫中的某些數據後在Chrome瀏覽器上。但我已經創建了所有他們獨立。這意味着,首先不上shopsAt屬性進行任何設置創建用戶,然後創建了一些房屋沒有設置任何的購物者屬性,然後創建了一些UserStores

重要的問題: 在創建UserStore的,我必須恢復實際的用戶和存儲以設置關係的反轉側?

不只是增加正確的鑰匙,UserStore和學說用戶/存儲的讀取期間將做比賽,因爲UserStore的創建是不夠,因爲,因爲它是關係的擁有方?

我必須手動修改由ORM產生的實體自動:生成實體命令和在所述方法中setStore和UserStore的SETUSER更新倒轉側?如前所述Here

我的數據庫看起來是這樣的:

USER: 

CREATE TABLE users (id INTEGER NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(50) NOT NULL, password_hash VARCHAR(255) NOT NULL, api_key VARCHAR(32) NOT NULL, status INTEGER NOT NULL, createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, PRIMARY KEY(id)) 

ROW 1 = "1","user1","[email protected]","xxxxxxxxxxxxxxxx","yyyyyyyyyyyyyy","1","2015-06-27 17:16:01","2015-06-27 17:16:01" 
ROW 2 = "2","user2","[email protected]","xxxxxxxxxxxxxxxx","yyyyyyyyyyyyyy","1","2015-06-27 17:38:36","2015-06-27 17:38:36" 

STORE: 

CREATE TABLE stores (id INTEGER NOT NULL, brand_id INTEGER DEFAULT NULL, neighborhood_id INTEGER DEFAULT NULL, county_id INTEGER DEFAULT NULL, city_id INTEGER DEFAULT NULL, state_id INTEGER DEFAULT NULL, streetname VARCHAR(255) NOT NULL COLLATE BINARY, streetnumber VARCHAR(255) NOT NULL COLLATE BINARY, cp VARCHAR(255) NOT NULL COLLATE BINARY, lat VARCHAR(255) NOT NULL COLLATE BINARY, lng VARCHAR(255) NOT NULL COLLATE BINARY, favorite INTEGER NOT NULL, status INTEGER NOT NULL, createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_D5907CCC44F5D008 FOREIGN KEY (brand_id) REFERENCES brands (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_D5907CCC803BB24B FOREIGN KEY (neighborhood_id) REFERENCES neighborhoods (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_D5907CCC85E73F45 FOREIGN KEY (county_id) REFERENCES counties (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_D5907CCC8BAC62AF FOREIGN KEY (city_id) REFERENCES cities (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_D5907CCC5D83CC1 FOREIGN KEY (state_id) REFERENCES states (id) NOT DEFERRABLE INITIALLY IMMEDIATE) 

ROW 1 = "1","1","1","1","1","1","streetname 1","11111","1234","0.0000000","1.1111111","1","1","2015-06-06 02:32:19","2015-06-06 02:32:19" 
ROW 2 = "2","4","2","2","2","2","streetname 2","2222","1234","0.000000","1.000000","0","1","2015-06-27 17:50:24","2015-06-27 17:50:24" 

USERSTORE: 

CREATE TABLE UsersStores (user_id INTEGER NOT NULL, store_id INTEGER NOT NULL, status INTEGER NOT NULL, createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL, PRIMARY KEY(user_id, store_id), CONSTRAINT FK_39EFBEF8A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_39EFBEF8B092A811 FOREIGN KEY (store_id) REFERENCES stores (id) NOT DEFERRABLE INITIALLY IMMEDIATE) 

VALUES: 
ROW 1 = "2","1","1","2015-06-28 17:51:40","2015-06-28 17:51:40" 
ROW 2 = "1","2","1","2015-06-28 17:51:51","2015-06-28 17:51:51" 
ROW 3 = "1","1","1","2015-06-28 20:51:53","2015-06-28 20:51:53" 



User: 
    type: entity 
    table: users 

    id: 
    id: 
     type: integer 
     generator: 
     strategy: auto 
    fields: 
    name: 
     type: string(255) 
    email: 
     type: string(50) 
    password_hash: 
     type: string(255) 
    api_key: 
     type: string(32) 
    status: 
     type: integer(1) 
    createdAt: 
     type: datetime 
    updatedAt: 
     type: datetime 

    oneToMany: 
    shopsAt: 
     targetEntity: UserStore 
     mappedBy: product 

/////////////////////////////////// 

Store: 
    type: entity 
    table: stores 
    id: 
    id: 
     type: integer 
     generator: 
     strategy: AUTO 
    fields: 
    streetname: 
     type: string 
    streetnumber: 
     type: string(255) 
    cp: 
     type: string(255) 
    lat: 
     type: string(255) 
    lng: 
     type: string(255) 
    favorite: 
     type: integer(1) 
    status: 
     type: integer(1) 
    createdAt: 
     type: datetime 
    updatedAt: 
     type: datetime 

    oneToMany: 
    productsAvailable: 
     targetEntity: ProductStore 
     mappedBy: store 
    shoppers: 
     targetEntity: UserStore 
     mappedBy: store 

    manyToOne: 
    brand: 
     targetEntity: Brand 
     joinColumn: 
     name: brand_id 
     referencedColumnName: id 
    neighborhood: 
     targetEntity: Neighborhood 
     joinColumn: 
     name: neighborhood_id 
     referencedColumnName: id 
    county: 
     targetEntity: County 
     joinColumn: 
     name: county_id 
     referencedColumnName: id 
    city: 
     targetEntity: City 
     joinColumn: 
     name: city_id 
     referencedColumnName: id 
    state: 
     targetEntity: State 
     joinColumn: 
     name: state_id 
     referencedColumnName: id 


////////////////////////////////////// 

UserStore: 
    type: entity 
    table: UsersStores 

    id: 
    user: 
     associationKey: true 
    store: 
     associationKey: true 
    fields: 
    status: 
     type: integer(1) 
    createdAt: 
     type: datetime 
    updatedAt: 
     type: datetime 

    manyToOne: 
    user: 
     targetEntity: User 
     inversedBy: shopsAt 
     joinColumn: 
     name: user_id 
     referencedColumnName: id 
    store: 
     targetEntity: Store 
     inversedBy: shoppers 
     joinColumn: 
     name: store_id 
     referencedColumnName: id 


////////////////////////////////////// 

use Doctrine\ORM\Mapping as ORM; 

/** 
* User 
*/ 
class User 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @var string 
    */ 
    private $name; 

    /** 
    * @var string 
    */ 
    private $email; 

    /** 
    * @var string 
    */ 
    private $password_hash; 

    /** 
    * @var string 
    */ 
    private $api_key; 

    /** 
    * @var integer 
    */ 
    private $status; 

    /** 
    * @var \DateTime 
    */ 
    private $createdAt; 

    /** 
    * @var \DateTime 
    */ 
    private $updatedAt; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return User 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set email 
    * 
    * @param string $email 
    * @return User 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 

     return $this; 
    } 

    /** 
    * Get email 
    * 
    * @return string 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Set password_hash 
    * 
    * @param string $passwordHash 
    * @return User 
    */ 
    public function setPasswordHash($passwordHash) 
    { 
     $this->password_hash = $passwordHash; 

     return $this; 
    } 

    /** 
    * Get password_hash 
    * 
    * @return string 
    */ 
    public function getPasswordHash() 
    { 
     return $this->password_hash; 
    } 

    /** 
    * Set api_key 
    * 
    * @param string $apiKey 
    * @return User 
    */ 
    public function setApiKey($apiKey) 
    { 
     $this->api_key = $apiKey; 

     return $this; 
    } 

    /** 
    * Get api_key 
    * 
    * @return string 
    */ 
    public function getApiKey() 
    { 
     return $this->api_key; 
    } 

    /** 
    * Set status 
    * 
    * @param integer $status 
    * @return User 
    */ 
    public function setStatus($status) 
    { 
     $this->status = $status; 

     return $this; 
    } 

    /** 
    * Get status 
    * 
    * @return integer 
    */ 
    public function getStatus() 
    { 
     return $this->status; 
    } 

    /** 
    * Set createdAt 
    * 
    * @param \DateTime $createdAt 
    * @return User 
    */ 
    public function setCreatedAt($createdAt) 
    { 
     $this->createdAt = $createdAt; 

     return $this; 
    } 

    /** 
    * Get createdAt 
    * 
    * @return \DateTime 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 

    /** 
    * Set updatedAt 
    * 
    * @param \DateTime $updatedAt 
    * @return User 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 

     return $this; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 
    /** 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    private $shopsAt; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->shopsAt = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Add shopsAt 
    * 
    * @param \UserStore $shopsAt 
    * @return User 
    */ 
    public function addShopsAt(\UserStore $shopsAt) 
    { 
     $this->shopsAt[] = $shopsAt; 

     return $this; 
    } 

    /** 
    * Remove shopsAt 
    * 
    * @param \UserStore $shopsAt 
    */ 
    public function removeShopsAt(\UserStore $shopsAt) 
    { 
     $this->shopsAt->removeElement($shopsAt); 
    } 

    /** 
    * Get shopsAt 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getShopsAt() 
    { 
     return $this->shopsAt; 
    } 
} 

/////////////////////////////////////// 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Store 
*/ 
class Store 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @var string 
    */ 
    private $streetname; 

    /** 
    * @var string 
    */ 
    private $streetnumber; 

    /** 
    * @var string 
    */ 
    private $cp; 

    /** 
    * @var string 
    */ 
    private $lat; 

    /** 
    * @var string 
    */ 
    private $lng; 

    /** 
    * @var integer 
    */ 
    private $favorite; 

    /** 
    * @var integer 
    */ 
    private $status; 

    /** 
    * @var \DateTime 
    */ 
    private $createdAt; 

    /** 
    * @var \DateTime 
    */ 
    private $updatedAt; 

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    private $productsAvailable; 

    /** 
    * @var \Brand 
    */ 
    private $brand; 

    /** 
    * @var \Neighborhood 
    */ 
    private $neighborhood; 

    /** 
    * @var \County 
    */ 
    private $county; 

    /** 
    * @var \City 
    */ 
    private $city; 

    /** 
    * @var \State 
    */ 
    private $state; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->productsAvailable = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set streetname 
    * 
    * @param string $streetname 
    * @return Store 
    */ 
    public function setStreetname($streetname) 
    { 
     $this->streetname = $streetname; 

     return $this; 
    } 

    /** 
    * Get streetname 
    * 
    * @return string 
    */ 
    public function getStreetname() 
    { 
     return $this->streetname; 
    } 

    /** 
    * Set streetnumber 
    * 
    * @param string $streetnumber 
    * @return Store 
    */ 
    public function setStreetnumber($streetnumber) 
    { 
     $this->streetnumber = $streetnumber; 

     return $this; 
    } 

    /** 
    * Get streetnumber 
    * 
    * @return string 
    */ 
    public function getStreetnumber() 
    { 
     return $this->streetnumber; 
    } 

    /** 
    * Set cp 
    * 
    * @param string $cp 
    * @return Store 
    */ 
    public function setCp($cp) 
    { 
     $this->cp = $cp; 

     return $this; 
    } 

    /** 
    * Get cp 
    * 
    * @return string 
    */ 
    public function getCp() 
    { 
     return $this->cp; 
    } 

    /** 
    * Set lat 
    * 
    * @param string $lat 
    * @return Store 
    */ 
    public function setLat($lat) 
    { 
     $this->lat = $lat; 

     return $this; 
    } 

    /** 
    * Get lat 
    * 
    * @return string 
    */ 
    public function getLat() 
    { 
     return $this->lat; 
    } 

    /** 
    * Set lng 
    * 
    * @param string $lng 
    * @return Store 
    */ 
    public function setLng($lng) 
    { 
     $this->lng = $lng; 

     return $this; 
    } 

    /** 
    * Get lng 
    * 
    * @return string 
    */ 
    public function getLng() 
    { 
     return $this->lng; 
    } 

    /** 
    * Set favorite 
    * 
    * @param integer $favorite 
    * @return Store 
    */ 
    public function setFavorite($favorite) 
    { 
     $this->favorite = $favorite; 

     return $this; 
    } 

    /** 
    * Get favorite 
    * 
    * @return integer 
    */ 
    public function getFavorite() 
    { 
     return $this->favorite; 
    } 

    /** 
    * Set status 
    * 
    * @param integer $status 
    * @return Store 
    */ 
    public function setStatus($status) 
    { 
     $this->status = $status; 

     return $this; 
    } 

    /** 
    * Get status 
    * 
    * @return integer 
    */ 
    public function getStatus() 
    { 
     return $this->status; 
    } 

    /** 
    * Set createdAt 
    * 
    * @param \DateTime $createdAt 
    * @return Store 
    */ 
    public function setCreatedAt($createdAt) 
    { 
     $this->createdAt = $createdAt; 

     return $this; 
    } 

    /** 
    * Get createdAt 
    * 
    * @return \DateTime 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 

    /** 
    * Set updatedAt 
    * 
    * @param \DateTime $updatedAt 
    * @return Store 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 

     return $this; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 

    /** 
    * Add productsAvailable 
    * 
    * @param \ProductStore $productsAvailable 
    * @return Store 
    */ 
    public function addProductsAvailable(\ProductStore $productsAvailable) 
    { 
     $this->productsAvailable[] = $productsAvailable; 

     return $this; 
    } 

    /** 
    * Remove productsAvailable 
    * 
    * @param \ProductStore $productsAvailable 
    */ 
    public function removeProductsAvailable(\ProductStore $productsAvailable) 
    { 
     $this->productsAvailable->removeElement($productsAvailable); 
    } 

    /** 
    * Get productsAvailable 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getProductsAvailable() 
    { 
     return $this->productsAvailable; 
    } 

    /** 
    * Set brand 
    * 
    * @param \Brand $brand 
    * @return Store 
    */ 
    public function setBrand(\Brand $brand = null) 
    { 
     $this->brand = $brand; 

     return $this; 
    } 

    /** 
    * Get brand 
    * 
    * @return \Brand 
    */ 
    public function getBrand() 
    { 
     return $this->brand; 
    } 

    /** 
    * Set neighborhood 
    * 
    * @param \Neighborhood $neighborhood 
    * @return Store 
    */ 
    public function setNeighborhood(\Neighborhood $neighborhood = null) 
    { 
     $this->neighborhood = $neighborhood; 

     return $this; 
    } 

    /** 
    * Get neighborhood 
    * 
    * @return \Neighborhood 
    */ 
    public function getNeighborhood() 
    { 
     return $this->neighborhood; 
    } 

    /** 
    * Set county 
    * 
    * @param \County $county 
    * @return Store 
    */ 
    public function setCounty(\County $county = null) 
    { 
     $this->county = $county; 

     return $this; 
    } 

    /** 
    * Get county 
    * 
    * @return \County 
    */ 
    public function getCounty() 
    { 
     return $this->county; 
    } 

    /** 
    * Set city 
    * 
    * @param \City $city 
    * @return Store 
    */ 
    public function setCity(\City $city = null) 
    { 
     $this->city = $city; 

     return $this; 
    } 

    /** 
    * Get city 
    * 
    * @return \City 
    */ 
    public function getCity() 
    { 
     return $this->city; 
    } 

    /** 
    * Set state 
    * 
    * @param \State $state 
    * @return Store 
    */ 
    public function setState(\State $state = null) 
    { 
     $this->state = $state; 

     return $this; 
    } 

    /** 
    * Get state 
    * 
    * @return \State 
    */ 
    public function getState() 
    { 
     return $this->state; 
    } 
    /** 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    private $shoppers; 


    /** 
    * Add shoppers 
    * 
    * @param \UserStore $shoppers 
    * @return Store 
    */ 
    public function addShopper(\UserStore $shoppers) 
    { 
     $this->shoppers[] = $shoppers; 

     return $this; 
    } 

    /** 
    * Remove shoppers 
    * 
    * @param \UserStore $shoppers 
    */ 
    public function removeShopper(\UserStore $shoppers) 
    { 
     $this->shoppers->removeElement($shoppers); 
    } 

    /** 
    * Get shoppers 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getShoppers() 
    { 
     return $this->shoppers; 
    } 
} 


////////////////////////////////////////////////// 


use Doctrine\ORM\Mapping as ORM; 

/** 
* UserStore 
*/ 
class UserStore 
{ 
    /** 
    * @var integer 
    */ 
    private $status; 

    /** 
    * @var \DateTime 
    */ 
    private $createdAt; 

    /** 
    * @var \DateTime 
    */ 
    private $updatedAt; 

    /** 
    * @var \User 
    */ 
    private $user; 

    /** 
    * @var \Store 
    */ 
    private $store; 


    /** 
    * Set status 
    * 
    * @param integer $status 
    * @return UserStore 
    */ 
    public function setStatus($status) 
    { 
     $this->status = $status; 

     return $this; 
    } 

    /** 
    * Get status 
    * 
    * @return integer 
    */ 
    public function getStatus() 
    { 
     return $this->status; 
    } 

    /** 
    * Set createdAt 
    * 
    * @param \DateTime $createdAt 
    * @return UserStore 
    */ 
    public function setCreatedAt($createdAt) 
    { 
     $this->createdAt = $createdAt; 

     return $this; 
    } 

    /** 
    * Get createdAt 
    * 
    * @return \DateTime 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 

    /** 
    * Set updatedAt 
    * 
    * @param \DateTime $updatedAt 
    * @return UserStore 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 

     return $this; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 

    /** 
    * Set user 
    * 
    * @param \User $user 
    * @return UserStore 
    */ 
    public function setUser(\User $user) 
    { 
     $this->user = $user; 

     return $this; 
    } 

    /** 
    * Get user 
    * 
    * @return \User 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 

    /** 
    * Set store 
    * 
    * @param \Store $store 
    * @return UserStore 
    */ 
    public function setStore(\Store $store) 
    { 
     $this->store = $store; 

     return $this; 
    } 

    /** 
    * Get store 
    * 
    * @return \Store 
    */ 
    public function getStore() 
    { 
     return $this->store; 
    } 

} 
+1

用於調試的快速提示:嘗試'dump($ value);'而不是'echo $ value'。 –

+0

謝謝托馬斯。您的評論是有用的,轉儲沒有工作,但我按照你的想法,並發現在PHP文檔var_dump。在使用它之後,它顯示了一個幾乎不可能理解的巨大結構,但是我能夠看到關係中使用的所有字段,並且發現了錯誤。在我的用戶YML文件被設置爲mappedBy:產品,應該是mappedBy:用戶。再一次,謝謝 – Gatunox

+0

哦,對不起,它是'var_dump($ value);'當然。 * dump *是我使用的調試工具的自定義函數。無論如何,我很高興它幫助你。 –

回答

0

當你撥打:

$用戶 - > getShopsAt()

您將收到回單\ Doctrine \ Common \ Collections \ ArrayCollection()表示一組結果的對象。您可以通過像這樣重複:

foreach ($User->getShopsAt() as $user_store) { 
    echo $user_store->getStatus(); 
    echo $user_store->getCreate at(); 
} 

等,或考多少結果是這樣的:

$User->getShopsAt()->count() 
count($User->getShopsAt()) <-- note I am NOT wrapping it in [] 

當你試圖在你上面的例子來算,你在包裝的對象[]出於某種原因,因此它正在計算該數組中元素的數量 - 即1,一個ArrayCollection對象。

編輯:

如果你真的想用一個數組,你可以這樣做:

$user->getShopsAt()->toArray() 

將轉換UserStore的ArrayCollection的對象到UserStore對象的簡單數組。