1

I am trying to insert data into db ,but it shows some error like thisImage of the error

My model Entity

Request.php

 is here `<?php


namespace EvolisClientRequest\Model\Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Request
{
    /**
     * @var \Ramsey\Uuid\Uuid
     * @ORM\Id
     * @ORM\Column(type="uuid")
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Salesperson", inversedBy="request")
     * @ORM\JoinTable(name="request_salesperson")
     * @var Salesperson
     */
    private $salesperson;

    /**
     * @ORM\ManyToOne(targetEntity="Client", inversedBy="request")
     * @var Client
     */
    private $client;

    /**
     * @ORM\ManyToMany(targetEntity="Status", inversedBy="request")
     * @ORM\JoinTable(name="request_status")
     * @var Status
     */
    private $status;

    /**
     * @ORM\Column(type="integer")
     * @var Qualification
     */
    private $qualification;

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

    /**
     * @return Salesperson
     */
    public function getSalesperson()
    {
        return $this->salesperson;
    }

    /**
     * @param Salesperson $salesperson
     */
    public function setSalesperson($salesperson)
    {
        $this->salesperson = $salesperson;
    }

    /**
     * @return Client
     */
    public function getClient()
    {
        return $this->client;
    }

    /**
     * @param Client $client
     */
    public function setClient($client)
    {
        $this->client = $client;
    }

    /**
     * @return Status
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param Status $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    /**
     * @return Qualification
     */
    public function getQualification()
    {
        return $this->qualification;
    }

    /**
     * @param Qualification $qualification
     */
    public function setQualification($qualification)
    {
        $this->qualification = $qualification;
    }

    public function __construct($salesperson, $client, $status, $qualification) {
        $this->salesperson = $salesperson;
        $this->client = $client;
        $this->status = $status;
        $this->qualification = $qualification;
    }

}`

Also my

DAO "RequestBaseDao.php" is here,which is automatically generated.

    <?php

/*
* This file has been automatically generated by Mouf/ORM.
* DO NOT edit this file, as it might be overwritten.
* If you need to perform changes, edit the RequestDao class instead!
*/

namespace EvolisClientRequest\Model\DAOs;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Mouf\Doctrine\ORM\Event\SaveListenerInterface;
use EvolisClientRequest\Model\Entities\Request;

/**
 * The RequestBaseDao class will maintain the persistence of Request class into the request table.
 *
 * @method Request findByQualification($fieldValue, $orderBy = null, $limit = null, $offset = null)
 * @method Request findOneByQualification($fieldValue, $orderBy = null)
 * @method Request findBySurfaceMin($fieldValue, $orderBy = null, $limit = null, $offset = null)
 * @method Request findOneBySurfaceMin($fieldValue, $orderBy = null)
 * @method Request findBySurfaceMax($fieldValue, $orderBy = null, $limit = null, $offset = null)
 * @method Request findOneBySurfaceMax($fieldValue, $orderBy = null)
 * @method Request findByPriceMin($fieldValue, $orderBy = null, $limit = null, $offset = null)
 * @method Request findOneByPriceMin($fieldValue, $orderBy = null)
 * @method Request findByPriceMax($fieldValue, $orderBy = null, $limit = null, $offset = null)
 * @method Request findOneByPriceMax($fieldValue, $orderBy = null)
 * @method Request findByRequestDate($fieldValue, $orderBy = null, $limit = null, $offset = null)
 * @method Request findOneByRequestDate($fieldValue, $orderBy = null)

 */
class RequestBaseDao extends EntityRepository
{

    /**
     * @var SaveListenerInterface[]
     */
    private $saveListenerCollection;

    /**
     * @param EntityManagerInterface $entityManager
     * @param SaveListenerInterface[] $saveListenerCollection
     */
    public function __construct(EntityManagerInterface $entityManager, array $saveListenerCollection = [])
    {
        parent::__construct($entityManager, $entityManager->getClassMetadata('EvolisClientRequest\Model\Entities\Request'));
        $this->saveListenerCollection = $saveListenerCollection;
    }

    /**
     * Get a new persistent entity
     * @param ...$params
     * @return Request
     */
    public function create(...$params) : Request
    {
        $entity = new Request(...$params);
        $this->getEntityManager()->persist($entity);
        return $entity;
    }

    /**
     * Peforms a flush on the entity.
     *
     * @param Request
     * @throws \Exception
     */
    public function save(Request $entity)
    {
        foreach ($this->saveListenerCollection as $saveListener) {
            $saveListener->preSave($entity);
        }

        $this->getEntityManager()->flush($entity);

        foreach ($this->saveListenerCollection as $saveListener) {
            $saveListener->postSave($entity);
        }

    }

    /**
     * Peforms remove on the entity.
     *
     * @param Request $entity
     */
    public function remove(Request $entity)
    {
        $this->getEntityManager()->remove($entity);
    }

    /**
     * Finds only one entity. The criteria must contain all the elements needed to find a unique entity.
     * Throw an exception if more than one entity was found.
     *
     * @param array $criteria
     *
     * @return Request
     */
    public function findUniqueBy(array $criteria) : Request
    {
        $result = $this->findBy($criteria);

        if (count($result) === 1) {
            return $result[0];
        } elseif (count($result) > 1) {
            throw new NonUniqueResultException('More than one Request was found');
        } else {
            return;
        }
    }

    /**
     * Finds only one entity by Qualification.
     * Throw an exception if more than one entity was found.
     *
     * @param mixed $fieldValue the value of the filtered field
     *
     * @return Request
     */
    public function findUniqueByQualification($fieldValue)
    {
        return $this->findUniqueBy(array('qualification' => $fieldValue));
    }
}

My RequestDao.php where i can write queries.

    <?php
namespace EvolisClientRequest\Model\DAOs;
use EvolisClientRequest\Model\Entities\Request;


/**
* The RequestDao class will maintain the persistence of Request class into the request table.
*/
class RequestDao extends RequestBaseDao {

    /*** PUT YOUR SPECIFIC QUERIES HERE !! ***/

    public function setdata()
    {
        /*$product = new Request();
        $product->setStatus('Keyboard');
        $product->setClient('000000001ae10dda000000003c4667a6');
        $product->setSalesperson('Ergonomic and stylish!');
        $product->setQualification('1111');
        //var_dump($r);die();
        $em = $this->getEntityManager(); 
        $em->persist($product);
        $em->flush();*/
       $product= $this->create('Keyboard','000000001ae10dda000000003c4667a6','Ergonomic and stylish!','1111');
        $this->save($product);

    }
}

Finally my Controller "ContactController.php"

   <?php
namespace EvolisClientRequest\Controllers;

use EvolisClientRequest\Model\DAOs\ClientDao;
use EvolisClientRequest\Model\Entities\Client;
use EvolisClientRequest\Model\Entities\Clients;
use EvolisClientRequest\Model\DAOs\RequestDao;
use EvolisClientRequest\Model\Entities\Request;
use EvolisClientRequest\Model\Entities\Requests;
use EvolisClientRequest\Model\DAOs\SalespersonDao;
use EvolisClientRequest\Model\Entities\Salesperson;
use EvolisClientRequest\Model\Entities\Salespersons;
use Mouf\Mvc\Splash\Annotations\Get;
use Mouf\Mvc\Splash\Annotations\Post;
use Mouf\Mvc\Splash\Annotations\Put;
use Mouf\Mvc\Splash\Annotations\Delete;
use Mouf\Mvc\Splash\Annotations\URL;
use Mouf\Html\Template\TemplateInterface;
use Mouf\Html\HtmlElement\HtmlBlock;
use Psr\Log\LoggerInterface;
use \Twig_Environment;
use Mouf\Html\Renderer\Twig\TwigTemplate;
use Mouf\Mvc\Splash\HtmlResponse;
use Doctrine\DBAL\DriverManager;
use Zend\Diactoros\Response\JsonResponse;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * TODO: write controller comment
 */
class ContactController {

    /**
     * The logger used by this controller.
     * @var LoggerInterface
     */
    private $logger;

    /**
     * The template used by this controller.
     * @var TemplateInterface
     */
    private $template;

    /**
     * The header of the page.
     * @var HtmlBlock
     */
    private $header;


    /**
     * The main content block of the page.
     * @var HtmlBlock
     */
    private $content;

    /**
     * The Twig environment (used to render Twig templates).
     * @var Twig_Environment
     */
    private $twig;


    /**
     * Controller's constructor.
     * @param LoggerInterface $logger The logger
     * @param TemplateInterface $template The template used by this controller
     * @param HtmlBlock $content The main content block of the page
     * @param Twig_Environment $twig The Twig environment (used to render Twig templates)
     */
    public function __construct(LoggerInterface $logger, TemplateInterface $template, HtmlBlock $content, HtmlBlock $header, Twig_Environment $twig, ClientDao $clientDao, RequestDao $requestDao, SalespersonDao $salespersonDao) {
        $this->logger = $logger;
        $this->template = $template;
        $this->content = $content;
        $this->twig = $twig;
        $this->header = $header;
        $this->clientDao = $clientDao;
        $this->requestDao = $requestDao;
        $this->salespersonDao = $salespersonDao;
    }

    /**
     * @URL("new.html")

     */
    public function new() {
        // TODO: write content of action here

        // Let's add the twig file to the template.
        $this->content->addHtmlElement(new TwigTemplate($this->twig, 'views/contact/new.twig', array("message"=>"world")));
        $this->header->addHtmlElement(new TwigTemplate($this->twig, 'views/root/header.twig', array("message"=>"world")));
        return new HtmlResponse($this->template);
    }

    /**
     * @URL("saveData")
     * For Saving the data
     */
    public function saveData()
    {
        /*$newClient = $this->clientDao->create('hello', '[email protected]','8907263949');
        $this->clientDao->save($newClient);*/
        //$data = array();
        //$data['salespersonDao']['salesperson'] = '[email protected]';
        //$data['request']['qualification'] = 'abcdefgh';

        //$newClient = $this->requestDao->create($data);
        //$newClient = $this->requestDao->setQualification('Keyboard');
       // $this->requestDao->save($newClient);
        $user_data=$this->requestDao->setdata();

        //return new JsonResponse([ "status"=>0 ]);
    }
}

I am using Mouf framework.I am stuck with this problem.Someone Please help me to solve this problem. Thanks in advance

6
  • In Request class salesperson and status should be array collections, but you are assigning them as strings. Try using this example: $this->salesperson[] = $salesperson; and etc with status Commented Aug 9, 2016 at 6:18
  • I did not get that.. can you make it clear Commented Aug 9, 2016 at 7:45
  • ManyToMany and OneToMany relations are always array collections, so variables should be arrays (ex.: pastebin.com/EsJNGMrk) Commented Aug 9, 2016 at 7:52
  • So should I change this in my entity?I am new one in this ORM I am getting confused Commented Aug 9, 2016 at 7:56
  • Yes, yours salesperson and status setters should set array. Also, dont forget to change it in constructor. Ex.: $this->salesperson[] = $salesperson; Commented Aug 9, 2016 at 8:00

1 Answer 1

1

As advised by @rokas, you should really start reading more about Doctrine. This is not a Mouf issue, this is clearly a Doctrine ORM issue so the appropriate doc is here: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/index.html

Here are a few tips:

Your controller calls the setdata method

$user_data=$this->requestDao->setdata();

The setdata method calls:

$product= $this->create('Keyboard','000000001ae10dda000000003c4667a6','Ergonomic and stylish!','1111');

Now, the create method is:

public function create(...$params) : Request
{
    $entity = new Request(...$params);
    $this->getEntityManager()->persist($entity);
    return $entity;
}

This means that it will call the Request constructor with this parameters:

$entity = new Request('Keyboard','000000001ae10dda000000003c4667a6','Ergonomic and stylish!','1111');

Have a look at the Request constructor:

public function __construct($salesperson, $client, $status, $qualification) {
    $this->salesperson = $salesperson;
    $this->client = $client;
    $this->status = $status;
    $this->qualification = $qualification;
}

As you can see, the first parameter is $salesperson. You try to put the value "Keyboard" here. The $salesperson attribute is defined this way:

/**
 * @ORM\ManyToMany(targetEntity="Salesperson", inversedBy="request")
 * @ORM\JoinTable(name="request_salesperson")
 * @var Salesperson
 */
private $salesperson;

/**
 * @param Salesperson $salesperson
 */
public function setSalesperson($salesperson)
{
    $this->salesperson = $salesperson;
}

Now, here is your problem I think.

The $salesperson property is defined as a "ManyToMany" association. So you really cannot put a string in here, it is a collection of Salesperson. By the way, you should not "set" anything either. The setter should be completely removed.

Instead, you should consider using it as per the documentation here: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html

For instance:

$request->getSalesPersons()->add($someObjectRepresentingAPerson);

Also, notice that since the $salesperson represents a collection of Salesperson object, your should really name it $salesPersons (plural form)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.