New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayCollection always empty on "One-To-Many" side of the relationship #205

Closed
starchild2 opened this Issue Jul 9, 2015 · 6 comments

Comments

Projects
None yet
5 participants
@starchild2

starchild2 commented Jul 9, 2015

Hello, me again.
Working with zf-apigility-doctrine and two entities: Task.php and TaskDetail.php.

TaskDetail is child of Task and a Task can have multiple TaskDetails (Master/Detail concept).

This is my code:


details = new ArrayCollection();
  }
  
  //
  //  GETTERS AND SETTERS
  //
  public function getDetails() {
      return $this->details;
  }
  
  public function setDetails($details) {
  
  ```
  // set the task field on the detail to be sure the foreign_key on the
  // TaskDetail objects is set when the Task object is persisted.
  foreach ($details as $detail) {
      $detail->setTask($this);
  }
  
  $this->details = $details;
  ```
  
  }
  }
# 

task;
  }
  
  public function setTask($task) {
      $this->task = $task;
  }
  }
  # 

Then I proceed and create a service for Task with zf-apigility-doctrine admin ui but when I test the service if i get a taskdetail (calling /api/task-detail/1) in the json I have the parent "Task" entity inside the "_embedded" property. If I get a Task (calling /api/task/1) in the json I have a property called "details" but it is empty.

What am I doing wrong? Please help, thank you so much

@TomHAnderson

This comment has been minimized.

Contributor

TomHAnderson commented Jul 9, 2015

Use a hydrator strategy if you want to return inverse relationships. And
use max_deprh to avoid recurrsion.

On Thu, Jul 9, 2015, 10:16 AM starchild2 notifications@github.com wrote:

Hello, me again.
Working with zf-apigility-doctrine and two entities: Task.php and
TaskDetail.php.

TaskDetail is child of Task and a Task can have multiple TaskDetails
(Master/Detail concept).

This is my code:

details = new ArrayCollection(); } // // GETTERS AND SETTERS // public function getDetails() { return $this->details; } public function setDetails($details) { // set the task field on the detail to be sure the foreign_key on the // TaskDetail objects is set when the Task object is persisted. foreach ($details as $detail) { $detail->setTask($this); } $this->details = $details; } } task; } public function setTask($task) { $this->task = $task; } } Then I proceed and create a service for Task with zf-apigility-doctrine admin ui but when I test the service if i get a taskdetail (calling /api/task-detail/1) in the json I have the parent "Task" entity inside the "_embedded" property. If I get a Task (calling /api/task/1) in the json I have a property called "details" but it is empty. What am I doing wrong? Please help, thank you so much — Reply to this email directly or view it on GitHub https://github.com//issues/205.
@starchild2

This comment has been minimized.

starchild2 commented Jul 9, 2015

I was thinking about that, fact is I don't see the column "details" on the Admin UI... how can I add a hydrator strategy (what strategy is needed? Is there anything predefined to use?) if it does not appear in the ui?
Could you please dig into details? I'm a beginner and today I struggled with it so much...

Thank you!

@santiph

This comment has been minimized.

santiph commented Jul 16, 2015

@starchild2 under the module.config.php file of your module, you'll find a section for doctrine-hydrator.
In there, a collection of hydrator configurations for each serviced entity you've created will be found.
On the strategies array, you need to add a key/value pair indicating the attribute of the entity you want to hydrate, and the hydrator class you wish to use for it.
For example:

            'strategies' => array(
                'details' => 'ZF\\Apigility\\Doctrine\\Server\\Hydrator\\Strategy\\CollectionExtract',
            ),

Btw, @TomHAnderson what happened with the CollectionLink hydrator? It's not present on ZF\Apigility\Doctrine\Server\Hydrator\Strategy\ namespace any more.

@PowerKiKi

This comment has been minimized.

PowerKiKi commented Jul 22, 2015

@santiph this was extremely useful information. I spent a lot of time trying to achieve that. I still have a few question though:

I seem to get the exact same result by configuring zf-hal, instead of doctrine-hydrator, the following way:

<?php

return array(
    // ... other config
    'zf-hal' => array(
        'metadata_map' => array(
            'Doctrine\ORM\PersistentCollection' => array(
                'hydrator' => 'ArraySerializable',
                'isCollection' => true,
            ),
            // ... other config
        ),
    ),
);

Would you be able to explain what is the difference and which alternatives would be considered best practices ?

Also why is it not the behavior out of the box ? I would think that it is something that most people would like to implement. But maybe there are good reasons not to make it out of the box ? What downsides could there be ?

@evanb2

This comment has been minimized.

evanb2 commented Mar 4, 2016

How are we supposed to know what all of these settings are? Where is the documentation for all of this? Am I missing something?

@TomHAnderson

This comment has been minimized.

Contributor

TomHAnderson commented Mar 4, 2016

@evanb2 if there is something missing from the README.md contributions are welcome. You may find my blog useful: http://blog.tomhanderson.com and if you still require help you may find it at https://apiskeletons.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment