This is very basic: when working with objects you add a Client to an Order,
not a client-id. A client-id is something from the relational paradigm.
Adding foreign keys to a record is something that belongs to
database-thinking (and an Active Record pattern), you don't need an ORM for
that.
Recommended reading about DDD: Eric Evans , Vaughn Vernon, Martin Fowler's
PEAA.
On Sunday, 23 February 2014 23:56:53 UTC+1, Parsifal wrote:
>
> Finally I was able to finish wiki and I found my answers, just one answer,
> if someone answers just this one, then I am fine and ready to go....
>
>
> I am trying to insert a new Order row with doctrine, in Order entity I
> have clientId property that is ManyToOne to Client entity, because each
> client may have several orders.
> When I try to persist/flush a new Order, I cannot set a value just by
> $this->clientId = 1;
> But if I set mapping and functions below then I can add an order with
> clientId with:
> $client = $em->find('Entities\Client', 1);
> if ($client === null) {
> echo "No client found.\n";
> exit(1);
> }
> $order = new Entities\Order;
> $order->setClient($client);
> $order->setFields($_POST);
> $em->persist($order);
> $em->flush();
>
> Now my question is that if this is the only way to insert a new order?
> Isn't it possible to simply set a clientId to add a new order?
>
> Below is my mapping/functions I said above. and it works fine as I said, I
> am asking if there is no easier way to set clientId to add a new order
> without calling client entity before?
> From here
> https://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.htmlif
> I understood correctly it seems that yes, this is the only way to
> insert? Did I unerstand that page correctly?
> ========================================
> In Client.php, that is OneToManyClient, we have to have something like
> this:
> public function __construct() {
> $this->orders = new ArrayCollection();
> }
>
> public function addOrder($order)
> {
> $this->orders[] = $order;
> }
>
> public function setFields($fields)
> {
> foreach ($fields as $key=>$value) {
> $this->$key = $value;
> }
> }
> $metadata->mapOneToMany(array( 'fieldName' => 'orders',
> 'targetEntity' => 'Entities\\Order',
> 'mappedBy' => 'client',
> 'joinColumns' => array( 0 => array( 'name'
> => 'client_id',
>
> 'referencedColumnName' => 'client_id',
>
> 'nullable' => true,
>
> 'columnDefinition' => NULL,
> ), )
>
> ));
>
> ==
> In Order.php we have this:
>
> public function setClient($client)
> {
> $client->addOrder($this);
> $this->client = $client;
> }
>
> public function setFields($fields)
> {
> foreach ($fields as $key=>$value) {
> $this->$key = $value;
> }
> }
> $metadata->mapManyToOne(array( 'fieldName' => 'client',
> 'targetEntity' => 'Entities\\Client',
> 'inversedBy' => 'orders',
> 'joinColumns' => array( 0 => array( 'name'
> => 'client_id',
>
> 'referencedColumnName' => 'client_id',
>
> 'nullable' => true,
>
> 'columnDefinition' => NULL,
> ), )
>
> ));
>
> ==
>
> Then to insert $_POST order data to db for client_id = 1, we can do this:
> $client = $em->find('Entities\Client', 1);
> if ($client === null) {
> echo "No client found.\n";
> exit(1);
> }
> $order = new Entities\Order;
> $order->setClient($client);
> $order->setFields($_POST);
> $em->persist($order);
> $em->flush();
>
>
--
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/groups/opt_out.