public function testUserSubscription()
 {
     $user = $this->buildUser();
     $subscription = new Subscription();
     $subscription->setStatus(1);
     $subscription->setStarted(new \DateTime('NOW'));
     $user->setSubscription($subscription);
     $this->getEntityManager()->persist($user);
     $this->getEntityManager()->flush();
     //deve ter criado um id
     $this->assertNotNull($subscription->getId());
     $this->assertEquals(1, $subscription->getId());
     $savedUser = $this->getEntityManager()->find(get_class($user), $user->getId());
     $this->assertInstanceOf(get_class($subscription), $savedUser->getSubscription());
     $this->assertEquals($subscription->getId(), $savedUser->getSubscription()->getId());
 }
Пример #2
0
<?php

require 'bootstrap.php';
use DoctrineNaPratica\Model\User;
use DoctrineNaPratica\Model\Subscription;
$steve = $entityManager->getRepository('DoctrineNaPratica\\Model\\User')->find(1);
if (!$steve) {
    $steve = new User();
    $steve->setName('Steve Jobs');
    $steve->setLogin('sjobs');
    $steve->setEmail('*****@*****.**');
    $steve->setAvatar('steve.jpg');
    $subscription = new Subscription();
    $subscription->setUser($steve);
    $subscription->setStatus(1);
    $subscription->setStarted(new \Datetime());
    $steve->setSubscription($subscription);
    $entityManager->persist($subscription);
}
$steve->setName('Steve Wonder');
$entityManager->persist($steve);
echo $steve->getName(), "\n";
$bill = $entityManager->getRepository('DoctrineNaPratica\\Model\\User')->findOneBy(array('login' => 'bill'));
if (!$bill) {
    $bill = new User();
    $bill->setName('Bill Gates');
    $bill->setLogin('bill');
    $bill->setEmail('*****@*****.**');
    $bill->setAvatar('bill.jpg');
    $entityManager->persist($bill);
}