mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
findAll doesn't return all the records #4980
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @assoum891 on GitHub (Jan 18, 2016).
Originally assigned to: @Ocramius on GitHub.
when i do findAll on my entity repository i just get the first record of the database although i have two records in the database, but when i override the findAll method into my repo and i create the query with all the 'leftjoins' of my relations i get all the records...
@Jean85 commented on GitHub (Jan 18, 2016):
Can you give us a reproducible case, or at least some of your code?
@assoum891 commented on GitHub (Jan 19, 2016):
Actually i have an entity called Offer that has oneToMany relations with an entity called Account, when i call findAll() on my repository class it just returns the first record of the database but when i override the method and do this
public function findAll() { return $this->createQueryBuilder('o') ->select('o, sa, sda, a') ->leftJoin('o.sponsorAccount', 'sa') ->leftJoin('o.sponsoredAccount', 'sda') ->leftJoin('o.account', 'a') ->getQuery() ->getResult(); }i get all the records.
@Jean85 commented on GitHub (Jan 19, 2016):
What if you remove the others joins? (sponsorAccount, sponsoredAccount)
Are you sure about your relations? The Doctrine schema is valid?
@assoum891 commented on GitHub (Jan 19, 2016):
I don't think there is something wrong with my schema,
/** * @var Account * @ORM\ManyToOne(targetEntity="AccountNamespace...") * @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false) */ protected $account; /** * @var Account * @ORM\ManyToOne(targetEntity="AccountNamespace...") * @ORM\JoinColumn(name="sponsored_account_id", referencedColumnName="id", nullable=true) */ protected $sponsoredAccount; /** * @var Account * @ORM\ManyToOne(targetEntity="AccountNamespace...") * @ORM\JoinColumn(name="sponsor_account_id", referencedColumnName="id", nullable=true) */ protected $sponsorAccount;I've just tried removing the joins of sponsorAccount and sponsoredAccount, still one result in the list view...
@Jean85 commented on GitHub (Jan 19, 2016):
Ok, you have a ManyToOne, not a OneToMany as you stated in your previous comment. That's what was throwing me off.
Can you try this implementation? What happens with this?
@assoum891 commented on GitHub (Jan 19, 2016):
oh sorry i meant ManyToOne, and yes i tried that, i still don't get all the records in my list
@Jean85 commented on GitHub (Jan 19, 2016):
So that's your problem: you have a discrepancy between your schema and your database. The
findAll()assumes that your join is not nullable (as from your definition) and does a JOIN instead of a LEFT JOIN as you're doing.If you check your DB, you probably have 1 Offer with a NOT NULL on account_id, and the others with null.
You can close this issue.
@assoum891 commented on GitHub (Jan 19, 2016):
I dont understand.. actually the account is not supposed to be null, it's a mandatory information and all the records have not null account_id values, it's the sponsored_account_id and sponsor_account_id columns that can be null..and even the first record returned when i do just findAll() have a null sponsor_account_id
@Jean85 commented on GitHub (Jan 19, 2016):
I reduced your problem to this difference:
With this, you do a LEFT JOIN (like you did before), and you should obtain all the offers:
With this (my suggestion), you do a INNER JOIN, like the native
findAll()is doing, and you are retrieving just one row.The join is the only change, so the problem is in the relation's definition. If you're sure that all your rows in you offer table have a not null value on that field, you could check your inverse relation's definition.
@Ocramius commented on GitHub (Jan 20, 2016):
I initially thought this was about the ORM's provided
Doctrine\ORM\EntityRepository#findAll()API, but that's not the case.Therefore closing as
Invalid: this is not a bug.@GreeKatrina commented on GitHub (Mar 20, 2017):
I ran into this issue as well.
In my case, the User's company_id was either 0 or an id of a Company that didn't exist in the db (I don't remember which). I don't actually "require" the company_id, it can be 0 if the User doesn't belong to a company.
Is there a way to say that the company_id cannot be null, but is not required (as in, do a left instead of an inner join)?
I'm sure many people will be using Doctrine for existing DBs that don't have clean data (ex: a User's company_id doesn't exist in the company table). Should they expect an empty result for queries that fit this criteria?
@Ocramius commented on GitHub (Mar 20, 2017):
Use an SQL filter. Also, check https://stackoverflow.com/questions/15502408/doctrine-2-use-default-0-values-instead-of-null-for-relation
@GreeKatrina commented on GitHub (Mar 20, 2017):
In the BasicEntityPersister class and the
getJoinSQLForJoinColumns()method, would it be possible to add a "required" check or something like this?From the docs it defines
nullableunder the @Column annotation as:But under @JoinColumn it takes on a second "required" meaning:
I think it makes more sense to keep
nullabledefined the same for@Columnand@JoinColumn, and then add a second option (required) to@JoinColumn.It seems like this may already be possible after reading the docs on
columnDefinition, but I'm not sure how to use it and how the overriding works:@lcobucci commented on GitHub (Mar 20, 2017):
@GreeKatrina although an ORM can be used on applications that don't have clear data it shouldn't be designed with that intent.
A
nullableassociation is an optional one and that's how doctrine manages that.requiredadds complexity for no good reason (since well designed schemas will always have consistent data).What I'd do in your case is create a new nullable column (with a proper FK), migrate the data that can be migrated without causing an constraint error and then start using it consistently. Just drop the old column when you're sure it's no longer relevant.
@Ocramius commented on GitHub (Mar 21, 2017):
You can poke at it as much as you like, but such an atrocity is not landing
in the ORM anytime.
Use NULL where it is supposed to be used: it is a fair assumption and it
will not lead yo further complications.
You can make the ORM compatible with your older code via writable views and
triggers instead (proper BC and FC approach).
On 20 Mar 2017 19:58, "Katrina Theodosopoulos" notifications@github.com
wrote:
@GreeKatrina commented on GitHub (Mar 21, 2017):
@lcobucci @Ocramius Fair enough. Is it possible to set nullable to true for
@JoinColumn, and then do something withcolumnDefinitionthat forces something likecompany_id IS NOT NULL? I wasn't sure if that's what it meant in the docs by "slightly different column definitions for joining columns, for example regarding NULL/NOT NULL defaults".@Ocramius commented on GitHub (Mar 21, 2017):
@GreeKatrina that is possible, but it will still crash the ORM during load operations (due to failed association traversal -
NULLmeans something very specific to the ORM).