简体   繁体   中英

Symfony Doctrine, find() return proxies entity

Help, In controller I like usually $this->getDoctrine()->getManager()->getRepository('ArtelProfileBundle:Teams') ->find($id)

but have Proxies_CG_\\ProfileBundle\\Entity\\Teams why?? Because this is a PUT method and I use for new and old object but new object this is almost entity Artel\\ProfileBundle\\Entity\\Teams and have different with Proxies_CG_\\Artel\\ProfileBundle\\Entity\\Teams

example teamOld = Proxies_CG_\\ProfileBundle\\Entity\\Teams teamNew = Artel\\ProfileBundle\\Entity\\Teams

I try

        $qb = $this->getEntityManager()->createQueryBuilder('d');
    $qb
        ->select('d')
        ->from('ArtelProfileBundle:Teams', 'd')
        ->andWhere('d.id = :id')
        ->setParameter('id', $id)

        ->getQuery()
        ->getResult()
    ;
    $query = $qb->getQuery();
    $results = $query->getResult();
    return $results;

but still Proxies_CG_\\Artel\\ProfileBundle\\Entity\\Teams I try

$proxyObject->__load();

but only add information, but I need entity who to be Artel\\ProfileBundle\\Entity\\Teams for my service updateObject

    public function putTeamAction(Request $request, $id)
    {
    $teamOld = $this->getDoctrine()->getRepository('ArtelProfileBundle:Teams')->putTeamClient($id);
    if (!empty($user) && !empty($token)) {
        $data = $this->get('serializer')->serialize($data, 'json');

        $teamOld = $this->getDoctrine()->getManager()->getRepository('ArtelProfileBundle:Teams')
            ->find($id);

        $teamNew = $this->get('serializer')
            ->deserialize($data, 'Artel\ProfileBundle\Entity\Teams', 'json');

            $this->get('artel.project.update')->updateObject($teamOld, $teamNew);
            $this->getDoctrine()->getManager()->flush();
            return "Team successful update";
}

and I don't understand I have exactly PUT action for another entity project. why doctrine return another entity for same entity? for team -> proxies, project -> completely entity project? and in this action I don't have this problem team(proxies)

<?php
 namespace Artel\ProfileBundle\Entity;

 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
 use Gedmo\Mapping\Annotation as Gedmo;
 use FOS\ElasticaBundle\Configuration\Search;

/**
 * Teams
 *
 * @ORM\Table(name="teams")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 * @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\TeamsRepository")
 * @Search(repositoryClass="Artel\ProfileBundle\Entity\Repository\ArticleRepository")
 */

class Teams
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

project(completely entity)

  <?php
 namespace Artel\ProfileBundle\Entity;

 use Doctrine\ORM\Mapping as ORM,
  Gedmo\Mapping\Annotation as Gedmo,
  Symfony\Component\Validator\Constraints as Assert;
 use Artel\ProfileBundle\Helper\HelperMethod;
 use JMS\Serializer\Annotation\ExclusionPolicy;
 use JMS\Serializer\Annotation\Expose;
 use JMS\Serializer\Annotation\Type;
 /**
  * Project
  *
  * @ORM\Table(name="project")
  * @Gedmo\SoftDeleteable(fieldName="deletedAt")
  * @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository ProjectRepository")
  * @ExclusionPolicy("all")
  */
 class Project
 {
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @Expose()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

What is different o_0 ?

    public function updateObject($objectOld, $objectNew)
{
    if (get_class($objectOld) != get_class($objectNew)) {
        throw new \Exception('class not equals');
    }

    $accessor = new PropertyAccessor();

    $reflect = new \ReflectionClass($objectOld);
    $properties = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE);

    foreach ($properties as $property) {
        $propertyName = $property->getName();

        $newValue = $accessor->getValue($objectNew, $propertyName);

        if ($newValue !== null) {
            $accessor->setValue($objectOld, $propertyName, $newValue);
        }
    }

    return $objectOld;
}

UPDATE MAGIC when I deleted $id from param and add in parameters and in start action I findOneById I have completed entity. But when I deleted in start action findOneById again have proxies. And when I use for $sampleOld findOneById I have still proxies. Only when in start action $team = $this->getDoctrine()->getRepository('ArtelProfileBundle:Teams')->findOneById($team_id); I have completed and after $sampleOld = $manager->getRepository('ArtelProfileBundle:Teams') ->find($team_id); have completed entity. This a magic.

   public function putTeamAction(Request $request)
{
    $team_id = $this->get('request')->request->get('id');
    $team = $this->getDoctrine()->getRepository('ArtelProfileBundle:Teams')->findOneById($team_id);
    $manager = $this->getDoctrine()->getManager();
    $token = $this->get('request')->request->get('token');
    $user = $this->getDoctrine()->getRepository('ArtelProfileBundle:Users')->findOneBySecuritytoken($token);

    $data = $request->request->all();
    $view = View::create();

    if (!empty($user) && !empty($token)) {
        $data = $this->get('serializer')->serialize($data, 'json');

        $sampleOld = $manager->getRepository('ArtelProfileBundle:Teams')
            ->find($team_id);
        $sampleNew = $this->get('serializer')
            ->deserialize($data, 'Artel\ProfileBundle\Entity\Teams', 'json');

        if (!$sampleOld) {

            $manager->persist($sampleNew);
            $manager->flush();
            $view = $this->view('Project successful create', 200);
            return $this->handleView($view);

        } else {
            $this->get('artel.project.update')->updateObject($sampleOld, $sampleNew);
            $manager->flush();

            $view = $this->view('Project successful update', 200);
            return $this->handleView($view);
        }
    }else{
        $view = $this->view('Secret token is not valid', 101);
        return $this->handleView($view);
    }
}

but have Proxies_CG_\\ProfileBundle\\Entity\\Teams why??

This is normal doctrine behavior. You should change your updateObject method. I know it is universal, but has issues, when you try to update subclasses. Look at Memento pattern or implement update in entity class $entity->updateFrom(Entity $entity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM