I've been trying to solve this for a few days with no luck.


First a few clarifications:

Basically I want to create relation between "Users" and "Stores", My plan 
in to identify favorites shops to a particular user and Also from the Store 
perspective, who shops in one particular store as their favorite shop. My 
guess was to use a ManyToMany Relation, But after looking for examples 
about this, many sites describe that if you require a ManyToMany relation 
with additional fields (in my case I have 3 additional fields) you have to 
use OneToMany/ManyToOne instead


I'm using SQLITE for my DB just for development and I'm writing REST API in 
PHP using Slim Framework


Everything works fine when getting a particular entity using something like 
this. 

/**
     * 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;
    }}

or

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

or

$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 

Once I get any entity i can read their attributes. Except when i try to 
obtain the:


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

if I try 

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

if I try 

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

*If i understand the call to getShopsAt() should return all instance of 
"UserStore" entity that match the User id, But I'm not able to access any 
of its properties. Everything I've tried fails, Also i want to point out 
that I used the User Id "1" that has 2 entries on the "UserStore" Table. So 
why is count returning 1.*


 I've created the 3 Schema using YML format, since i find it to be more 
clear than PHP.


With these Schema i have created my entities using the command 
"vendor/bin/doctrine orm:generate-entities ./src" *(See resulting entities 
at the bottom)*


 Then I create the database schema using vendor/bin/doctrine 
orm:schema-tool:update --force *(See database SQL, and data at the bottom)*


 *After this I have created some data on the Database using the Advaced 
REST Client extension on Chrome Browser. BUT I've created all of them 
independently. This mean, First create the Users without setting anything 
on to shopsAt attribute, then created some Stores without setting anything 
to the shoppers attribute and then created some UserStores*


 *IMPORTANT QUESTIONS:* 

During the creation of UserStore i have to recover the actual User and 
Store to set their inversed side of the relation? 

or

No just adding the correct keys to the UserStore and Doctrine will do the 
match during a read of the User/Store, since the creation of UserStore is 
enough because because it's the Owning side of the relation?

or 
I have to manually modify the entity auto generated by the 
orm:generate-entities command and in the methods setStore and setUser of 
the UserStore to update the inversed side?

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to