T
T
TANK_IST2017-09-11 02:28:28
symfony
TANK_IST, 2017-09-11 02:28:28

Why doesn't persist work when loading fixtures?

There is an essence

<?php
namespace OwrBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="rating")
 */
class Rating
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="OwrBundle\Entity\User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="OwrBundle\Entity\User")
     * @ORM\JoinColumn(name="critic_id", referencedColumnName="user_id")
     */
    protected $critic;

    /**
     * @ORM\Column(type="integer")
     */
    protected $rating = 0;

    /**
     * @ORM\Column(type="integer")
     */
    protected $type = 0;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user)
    {
        $this->user = $user;
    }

    /**
     * @return mixed
     */
    public function getCritic()
    {
        return $this->critic;
    }

    /**
     * @param mixed $critic
     */
    public function setCritic($critic)
    {
        $this->critic = $critic;
    }

    /**
     * @return mixed
     */
    public function getRating()
    {
        return $this->rating;
    }

    /**
     * @param mixed $rating
     */
    public function setRating($rating)
    {
        $this->rating = $rating;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    public function __toString()
    {
        return strval($this->id);
    }
}

Fixtures
<?php
namespace OwrBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\{AbstractFixture, OrderedFixtureInterface};
use Doctrine\Common\Persistence\ObjectManager;
use OwrBundle\Entity\Rating;
use Symfony\Component\DependencyInjection\{ContainerInterface, ContainerAwareInterface};

class RatingFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function load(ObjectManager $manager)
    {
        $rat = array(
            ['user1', 'user2', 4, 0],
            ['user1', 'user3', 5, 1],
            ['user1', 'user2', 5, 2],
            ['user2', 'user1', 4, 0],
            ['user2', 'user3', 5, 1],
            ['user2', 'user1', 4, 1],
            ['user2', 'user3', 5, 2],
            ['user3', 'user1', 3, 0],
            ['user3', 'user2', 4, 0],
            ['user3', 'user1', 2, 2],
            ['user3', 'user2', 4, 2]
        );

        foreach ($rat as $val) {
            $rating = new Rating();
            $rating->setUser($manager->merge($this->getReference($val[0])));
            $rating->setCritic($manager->merge($this->getReference($val[1])));
            $rating->setRating($val[2]);
            $rating->setType($val[3]);

            $manager->persist($rating);
            print_r($val);
            echo '-';
            print_r($rating->getId());
            echo "\n-\n";
        }

        $manager->flush();
        $manager->clear();
    }

    public function getOrder()
    {
        return 19;
    }
}

But only the first three lines are loaded. print_r:
Array
(
    [0] => user1
    [1] => user2
    [2] => 4
    [3] => 0
)
-1
-
Array
(
    [0] => user1
    [1] => user3
    [2] => 5
    [3] => 1
)
-2
-
Array
(
    [0] => user1
    [1] => user2
    [2] => 5
    [3] => 2
)
-3
-
Array
(
    [0] => user2
    [1] => user1
    [2] => 4
    [3] => 0
)
-
-
Array
(
    [0] => user2
    [1] => user3
    [2] => 5
    [3] => 1
)
-
-
Array
(
    [0] => user2
    [1] => user1
    [2] => 4
    [3] => 1
)
-
-
Array
(
    [0] => user2
    [1] => user3
    [2] => 5
    [3] => 2
)
-
-
Array
(
    [0] => user3
    [1] => user1
    [2] => 3
    [3] => 0
)
-
-
Array
(
    [0] => user3
    [1] => user2
    [2] => 4
    [3] => 0
)
-
-
Array
(
    [0] => user3
    [1] => user1
    [2] => 2
    [3] => 2
)
-
-
Array
(
    [0] => user3
    [1] => user2
    [2] => 4
    [3] => 2
)
-
-

Why is that?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question