Skip to content
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

[DDC-3444] Refresh cascading of lazy loaded associations does not work #1218

Open
wants to merge 3 commits into
base: old-prototype-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/Doctrine/Tests/Models/DDC1987/DDC1987Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Doctrine\Tests\Models\DDC1987;

/**
* @Table(name="ddc1987_item")
* @Entity
*/
class DDC1987Item
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
public $id;

/**
* @Column(type="decimal", scale=4, precision=15, nullable=false)
*/
public $price;

/**
* @ManyToOne(targetEntity="DDC1987Order", inversedBy="items")
* @JoinColumn(name="order_id", referencedColumnName="order_id", onDelete="CASCADE", nullable=false)
*/
public $order;

public function __construct(DDC1987Order $order, $price)
{
$this->order = $order;
$this->price = $price;
}
}
35 changes: 35 additions & 0 deletions tests/Doctrine/Tests/Models/DDC1987/DDC1987Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Doctrine\Tests\Models\DDC1987;

/**
* @Table(name="ddc1987_order")
* @Entity
*/
class DDC1987Order
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @GeneratedValue
* @Column(type="integer", name="order_id")
*/
public $id;

/**
* @OneToMany(targetEntity="DDC1987Item", mappedBy="order", cascade={"refresh", "persist"})
*/
protected $items;

public function __construct($id)
{
$this->id = $id;
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}

public function getItems()
{
return $this->items;
}
}
73 changes: 73 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1987Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Query;

/**
* @group DDC-2494
*/
class DDC1987Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $gearman = null;
private $results = array();

protected function setUp()
{
if (!class_exists('GearmanClient', false)) {
$this->markTestSkipped('pecl/gearman is required for this test to run.');
}

$workers = shell_exec('(echo workers ; sleep 0.1) | netcat 127.0.0.1 4730');
if (substr_count($workers, 'incrementPrice') < 2) {
$this->markTestSkipped('At least two gearman workers are required for this test to run.');
}

parent::setUp();

$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(\Doctrine\Tests\Models\DDC1987\DDC1987Order::CLASSNAME),
$this->_em->getClassMetadata(\Doctrine\Tests\Models\DDC1987\DDC1987Item::CLASSNAME),
));

$this->tasks = array();

$this->gearman = new \GearmanClient();
$this->gearman->addServer();
$this->gearman->setCompleteCallback(array($this, "gearmanTaskCompleted"));

$order = new \Doctrine\Tests\Models\DDC1987\DDC1987Order(1);
$item = new \Doctrine\Tests\Models\DDC1987\DDC1987Item($order, 1);
$order->getItems()->add($item);

$this->_em->persist($order);
$this->_em->flush();
}

public function gearmanTaskCompleted($task)
{
$this->results[] = $task->data();

if (count($this->results) == 2) {
$this->assertTrue($this->results[1] == '3');
}
}

public function testIssue()
{
$this->gearman->addTask('incrementPrice', serialize(array(
'conn' => $this->_em->getConnection()->getParams(),
'delay' => 0
)));

$this->gearman->addTask('incrementPrice', serialize(array(
'conn' => $this->_em->getConnection()->getParams(),
'delay' => 1
)));

$this->gearman->runTasks();
}
}



99 changes: 99 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1987TestWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

class DDC1987TestWorker
{
private $em;

static public function run()
{
$testWorker = new DDC1987TestWorker();

$worker = new \GearmanWorker();
$worker->addServer();
$worker->addFunction("incrementPrice", array($testWorker, "incrementPrice"));

while($worker->work()) {
if ($worker->returnCode() != GEARMAN_SUCCESS) {
echo "return_code: " . $worker->returnCode() . "\n";
break;
}
}
}

public function incrementPrice($job)
{
$this->processWorkload($job);

// Fetch into memory
$order = $this->em->find('Doctrine\Tests\Models\DDC1987\DDC1987Order', 1);
$items = $order->getItems();
$item = $items->first();

// 'Old' value
echo '$item->price = '.$item->price.PHP_EOL;

// Some delay to allow for interleaving of the tasks
echo 'Sleep...'.PHP_EOL;
sleep(2);

// Refresh
echo 'Refresh..'.PHP_EOL;
$this->em->refresh($order);
//$this->em->refresh($item); // <---- By explicitly refreshing we fix the bug

// 'New' value
$item = $order->getItems()->first();
echo '$item->price = '.$item->price.PHP_EOL;

// Update
echo 'Changed $item->price to '.($item->price+1).PHP_EOL;
$item->price = $item->price+1;

echo 'Persist'.PHP_EOL;
$this->em->persist($order);
$this->em->flush();

return $item->price;
}

protected function processWorkload($job)
{
echo "Received job: " . $job->handle() . " for function " . $job->functionName() . "\n";

$workload = $job->workload();
$workload = unserialize($workload);

if (!isset($workload['conn']) || !is_array($workload['conn'])) {
throw new \InvalidArgumentException("Missing Database parameters");
}

// Allow for interleaving of tasks
sleep($workload['delay']);

$this->em = $this->createEntityManager($workload['conn']);
}

protected function createEntityManager($conn)
{
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir(__DIR__ . '/../../../Proxies');
$config->setProxyNamespace('MyProject\Proxies');
$config->setAutoGenerateProxyClasses(true);

$annotDriver = $config->newDefaultAnnotationDriver(array(__DIR__ . '/../../../Models/'), true);
$config->setMetadataDriverImpl($annotDriver);

$cache = new \Doctrine\Common\Cache\ArrayCache();
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());

$em = \Doctrine\ORM\EntityManager::create($conn, $config);

return $em;
}
}

DDC1987TestWorker::run();