Ticket #734 (new defect)
Opened 17 months ago
Counting on collections doesn't work as expected
| Reported by: | hans | Owned by: | hans |
|---|---|---|---|
| Priority: | normal | Milestone: | To be scheduled |
| Component: | Generator | Version: | 1.3.0 |
| Severity: | normal | Keywords: | |
| Cc: |
Description
From Sal Dekku on mailing list.
I have a class named Reference that is external key on the class named Links. The method Reference::countLinks() is not behaving as I expected: after the Reference::addLink() method is called, it returns only the number of fresh (not yet saved) links, and not the total number (stored + fresh ones)
Example:
$external = ReferencePeer::retrievebyPK(1); echo $external->countLinks().'<br>'; // = 2 $link = new Link; $external->addLink ($link); echo $external->countLinks().'<br>'; // should be 3 $link2 = new Link; $external->addLink ($link2); echo $external->countLinks().'<br>'; // should be 4
Since object no.1 already has 2 links, I would expect the output to be: 2 3 4
Instead it goes like: 2 1 2
Even calling $external->save() every time won't solve the problem, since the criteria for the count is the same, hence the query is not re-issued.
Workaround:
using Links::setReference()->save()
Code:
Following is part of the countLinks method, where I've added a comment to highlight what I suppose is the source of the problem:
} else {
// criteria has no effect for a new object
if (!$this->isNew()) {
$criteria->add(LinkPeer::REFERENCELINK, $this->id);
if (!isset($this->lastLinkCriteria) ||
!$this->lastLinkCriteria->equals($criteria)) {
$count = LinkPeer::doCount($criteria, $con);
} else {
// This seems wrong to me!
// If the Reference object is not new (it's in the DB)
// I should get docount(stored links) + count(fresh links)
// not just count(fresh links)
// Perhaps the docount value used to be stored somewhere after the first call?
$count = count($this->collLinks);
}
} else {
$count = count($this->collLinks);
}
}
Related:
System Specs:
Windows Vista Business - Apache 2.2.8 - PHP 5.2.6 - MySQL 5.051b