These are my 2 classes
class Bill implements Searchable
{
use \LaravelDoctrine\Scout\Indexable;
/**
* @SWG\Property(type="string", format="uuid")
* @var id
* @ORM\Id
* @ORM\Column(type="guid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Entities\BillIdGenerator")
*/
public $id;
/**
* @ORM\Column(type="text")
* @SWG\Property(type="string")
* @Serializer\Groups({"bills","bill"})
*/
protected $summary;
/**
* @ORM\OneToMany(targetEntity="BillFavorite", mappedBy="bill",
cascade={"persist", "remove"}, fetch="EXTRA_LAZY")
* @SWG\Property(type="array",
@SWG\Items(ref="#/definitions/BillFavorite"))
* @Exclude
*/
protected $favorites;
public function __construct()
{
$this->favorites = new ArrayCollection;
}
public function getId()
{
return $this->id;
}
public function getSummary()
{
return $this->summary;
}
public function setSummary($summary)
{
$this->summary = $summary;
}
}
class BillFavorite implements Searchable
{
use \LaravelDoctrine\Scout\Indexable;
/**
* @SWG\Property(type="string", format="uuid")
* @var id
* @ORM\Id
* @ORM\Column(type="guid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Entities\BillFavoriteIdGenerator")
*/
public $id;
/**
* @ORM\ManyToOne(targetEntity="Bill", inversedBy="favorites")
* @SWG\Property(type="object", ref="#/definitions/Bill")
*/
protected $bill;
/**
* @ORM\Column(type="integer", length=10, options={"unsigned"=true})
* @SWG\Property(type="integer")
*/
protected $user_id;
public function __construct()
{
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getBill()
{
return $this->bill;
}
public function setBill($bill)
{
$this->bill = $bill;
}
public function getUserId() {
return $this->user_id;
}
public function setUserId($user_id) {
$this->user_id = $user_id;
}
}
A Bill can have multiple favorites by different users, to give an example I
am making an api call say /api/bills
I want to return a paginated list of bills and I would like the 'favorties'
to show up but only the ones made by the user who is making the api
request, I do not want the user to see if other people have also favorited
the bill. What do you think would be the best course of action to achieve
this result?
On Tuesday, June 27, 2017 at 1:56:25 AM UTC-5, Marco Pivetta wrote:
>
> It will bite you back, as we will also add an exception for this in the
> ORM at some point
>
> On 27 Jun 2017 08:54, "Joshua Wilson" <[email protected] <javascript:>>
> wrote:
>
>> Lets assume the objects I get back, I do not care to update in the
>> request, consider them strictly read only, then does it matter if the data
>> is 'corrupted'? If it does not matter, I would like to do it lol.
>>
>> On Tuesday, June 27, 2017 at 1:14:41 AM UTC-5, Marco Pivetta wrote:
>>>
>>> You still cannot hydrate a `Bill` object with a subset of `Favorite`
>>> instances in it, as that would corrupt the data. You can use the good old
>>> SQL-style multi-column result:
>>>
>>>
>>> SELECT f as fav, b as bill FROM Favorite f JOIN f.bill b WHERE f.id IN
>>> (SELECT f1.id FROM Bill b JOIN b.favorites f1 WHERE f1.user = :user OR
>>> f1.user IS NULL)
>>>
>>> Marco Pivetta
>>>
>>> http://twitter.com/Ocramius
>>>
>>> http://ocramius.github.com/
>>>
>>> On Tue, Jun 27, 2017 at 8:11 AM, Joshua Wilson <[email protected]> wrote:
>>>
>>>> That would end up being a problem as I would only see a subset of bills
>>>> where as I want the complete list of bills with a subset of favorite, does
>>>> that make sense?
>>>>
>>>> On Tuesday, June 27, 2017 at 1:01:17 AM UTC-5, Marco Pivetta wrote:
>>>>>
>>>>> That's the same as
>>>>>
>>>>> SELECT b FROM Bill b LEFT JOIN b.favorites f WHERE f.user = :user OR
>>>>> f.user IS NULL
>>>>>
>>>>> That is a problem, as it will hydrate `Bill` objects with incomplete
>>>>> associations.
>>>>>
>>>>> Turn it around:
>>>>>
>>>>> SELECT f FROM Favorite f WHERE f.id IN (SELECT f1.id FROM Bill b JOIN
>>>>> b.favorites f1 WHERE f1.user = :user OR f1.user IS NULL)
>>>>>
>>>>>
>>>>> Marco Pivetta
>>>>>
>>>>> http://twitter.com/Ocramius
>>>>>
>>>>> http://ocramius.github.com/
>>>>>
>>>>> On Tue, Jun 27, 2017 at 7:58 AM, Joshua Wilson <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> Actaully, based on your first response, I went back at looked at the
>>>>>> criteria builder even more and ended up messing around with it and got a
>>>>>> working example:
>>>>>>
>>>>>> $qb = $this->bill_repository->createQueryBuilder('b')
>>>>>> ->leftJoin('b.favorites', 'f');
>>>>>>
>>>>>> $critera = new Criteria();
>>>>>> $critera->where(Criteria::expr()->eq("f.user_id", $user->id));
>>>>>> $critera->orWhere(Criteria::expr()->eq("f.user_id", null));
>>>>>> $qb->addCriteria($critera);
>>>>>>
>>>>>>
>>>>>> On Tuesday, June 27, 2017 at 12:43:24 AM UTC-5, Marco Pivetta wrote:
>>>>>>>
>>>>>>> Could you make a more clear example with your currently entity
>>>>>>> definitions? Keep it minimal, but try showing what the expected result
>>>>>>> would be like
>>>>>>>
>>>>>>> Marco Pivetta
>>>>>>>
>>>>>>> http://twitter.com/Ocramius
>>>>>>>
>>>>>>> http://ocramius.github.com/
>>>>>>>
>>>>>>> On Tue, Jun 27, 2017 at 7:40 AM, Joshua Wilson <[email protected]>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> I have but I dont quite understand how to work with it, in an ideal
>>>>>>>> world i'd basically like something like...
>>>>>>>>
>>>>>>>> SELECT a.*, b.* FROM table a LEFT JOIN table b ON b.column =
>>>>>>>> a.column WITH b.other_column = 1;
>>>>>>>>
>>>>>>>> Basically, at the end of the day, I have a table full of items,
>>>>>>>> users can favorite those items, but I dont want users to see what each
>>>>>>>> other have favorited when I display the list of items, I only want the
>>>>>>>> user
>>>>>>>> making the request to be able to see what items they have favorited.
>>>>>>>> Granted I can probably loop over the results from the query and check
>>>>>>>> each
>>>>>>>> record and mark if it was favorited but I already have a one to many
>>>>>>>> relationship setup between items and favorites and it'd be really nice
>>>>>>>> if I
>>>>>>>> could just return the single favorite record for the user who is
>>>>>>>> looking at
>>>>>>>> the list of items
>>>>>>>>
>>>>>>>> --
>>>>>>>> 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 https://groups.google.com/group/doctrine-user.
>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>> 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 https://groups.google.com/group/doctrine-user.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>> --
>>>> 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 https://groups.google.com/group/doctrine-user.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> 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] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/doctrine-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
--
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 https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.