2015-09-06 92 views
1

我有一個預算的形式從客戶端實體在select字段列出的數據。總結,預算和客戶是相互關聯的。預算有client_id列,即假設通過從所選客戶端獲取id值來存儲它。堅持與數據相關實體

<form action="{{ path('panel_add_budget') }}" method="POST"> 
    <strong>Cliente</strong> 
    <div class="form-group"> 
     <select name="cdg_budget_type[client_id]" class="form-control"> 
      {% for client in clients %} 
       <option value="{{ client.id }}">{{ client.name }}</option> 
      {% endfor %} 
     </select> 
    </div> 

    <div class="form-group"> 
     {{ form_row(form.address) }} 
    </div> 
    <div class="form-group"> 
     {{ form_row(form.installments) }} 
    </div> 
    <div class="form-group"> 
     {{ form_row(form.check_days) }} 
    </div> 
    <div class="form-group"> 
     {{ form_row(form.date_start) }} 
    </div> 
    <div class="form-group"> 
     {{ form_row(form.total_value) }} 
    </div> 

    <button type="submit" class="btn btn-success">Adicionar</button> 
</form> 

我的客戶單位:

class Client 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @var string 
    */ 
    private $name; 

    /** 
    * @var string 
    */ 
    private $phone; 

    /** 
    * @var string 
    */ 
    private $email; 

    /** 
    * @var string 
    */ 
    private $streetName; 

    /** 
    * @var string 
    */ 
    private $district; 

    /** 
    * @var string 
    */ 
    private $number; 

    /** 
    * @var string 
    */ 
    private $city; 

    /** 
    * @var string 
    */ 
    private $zipCode; 

    /** 
    * @var integer 
    */ 
    private $budget; 


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

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return Client 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set phone 
    * 
    * @param string $phone 
    * @return Client 
    */ 
    public function setPhone($phone) 
    { 
     $this->phone = $phone; 

     return $this; 
    } 

    /** 
    * Get phone 
    * 
    * @return string 
    */ 
    public function getPhone() 
    { 
     return $this->phone; 
    } 

    /** 
    * Set email 
    * 
    * @param string $email 
    * @return Client 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 

     return $this; 
    } 

    /** 
    * Get email 
    * 
    * @return string 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Set streetName 
    * 
    * @param string $streetName 
    * @return Client 
    */ 
    public function setStreetName($streetName) 
    { 
     $this->streetName = $streetName; 

     return $this; 
    } 

    /** 
    * Get streetName 
    * 
    * @return string 
    */ 
    public function getStreetName() 
    { 
     return $this->streetName; 
    } 

    /** 
    * Set district 
    * 
    * @param string $district 
    * @return Client 
    */ 
    public function setDistrict($district) 
    { 
     $this->district = $district; 

     return $this; 
    } 

    /** 
    * Get district 
    * 
    * @return string 
    */ 
    public function getDistrict() 
    { 
     return $this->district; 
    } 

    /** 
    * Set number 
    * 
    * @param string $number 
    * @return Client 
    */ 
    public function setNumber($number) 
    { 
     $this->number = $number; 

     return $this; 
    } 

    /** 
    * Get number 
    * 
    * @return string 
    */ 
    public function getNumber() 
    { 
     return $this->number; 
    } 

    /** 
    * Set city 
    * 
    * @param string $city 
    * @return Client 
    */ 
    public function setCity($city) 
    { 
     $this->city = $city; 

     return $this; 
    } 

    /** 
    * Get city 
    * 
    * @return string 
    */ 
    public function getCity() 
    { 
     return $this->city; 
    } 

    /** 
    * Set zipCode 
    * 
    * @param string $zipCode 
    * @return Client 
    */ 
    public function setZipCode($zipCode) 
    { 
     $this->zipCode = $zipCode; 

     return $this; 
    } 

    /** 
    * Get zipCode 
    * 
    * @return string 
    */ 
    public function getZipCode() 
    { 
     return $this->zipCode; 
    } 

    /** 
    * Set budget 
    * 
    * @param integer $budget 
    */ 
    function setBudget($budget) 
    { 
     $this->budget = $budget; 
    } 

    /** 
    * Get budget 
    * 
    * @return integer 
    */ 
    function getBudget() 
    { 
     return $this->budget; 
    } 
} 

經濟實體:

class Budget 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @var integer 
    */ 
    private $clientId; 

    /** 
    * @var string 
    */ 
    private $address; 

    /** 
    * @var integer 
    */ 
    private $installments; 

    /** 
    * @var integer 
    */ 
    private $checkDays; 

    /** 
    * @var \DateTime 
    */ 
    private $dateStart; 

    /** 
    * @var \DateTime 
    */ 
    private $dateCreated; 

    /** 
    * @var decimal 
    */ 
    private $totalValue; 

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

    /** 
    * Set clientId 
    * 
    * @param integer $clientId 
    * @return Budget 
    */ 
    public function setClientId($clientId) 
    { 
     $this->clientId = $clientId; 

     return $this; 
    } 

    /** 
    * Get clientId 
    * 
    * @return integer 
    */ 
    public function getClientId() 
    { 
     return $this->clientId; 
    } 

    /** 
    * Set address 
    * 
    * @param string $address 
    * @return Budget 
    */ 
    public function setAddress($address) 
    { 
     $this->address = $address; 

     return $this; 
    } 

    /** 
    * Get address 
    * 
    * @return string 
    */ 
    public function getAddress() 
    { 
     return $this->address; 
    } 

    /** 
    * Set installments 
    * 
    * @param integer $installments 
    * @return Budget 
    */ 
    public function setInstallments($installments) 
    { 
     $this->installments = $installments; 

     return $this; 
    } 

    /** 
    * Get installments 
    * 
    * @return integer 
    */ 
    public function getInstallments() 
    { 
     return $this->installments; 
    } 

    /** 
    * Set checkDays 
    * 
    * @param integer $checkDays 
    * @return Budget 
    */ 
    public function setCheckDays($checkDays) 
    { 
     $this->checkDays = $checkDays; 

     return $this; 
    } 

    /** 
    * Get checkDays 
    * 
    * @return integer 
    */ 
    public function getCheckDays() 
    { 
     return $this->checkDays; 
    } 

    /** 
    * Set dateStart 
    * 
    * @param \DateTime $dateStart 
    * @return Budget 
    */ 
    public function setDateStart($dateStart) 
    { 
     $this->dateStart = $dateStart; 

     return $this; 
    } 

    /** 
    * Get dateStart 
    * 
    * @return \DateTime 
    */ 
    public function getDateStart() 
    { 
     return $this->dateStart; 
    } 

    /** 
    * Set dateCreated 
    * 
    * @param \DateTime $dateCreated 
    * @return Budget 
    */ 
    public function setDateCreated($dateCreated) 
    { 
     $this->dateCreated = $dateCreated; 

     return $this; 
    } 

    /** 
    * Get dateCreated 
    * 
    * @return \DateTime 
    */ 
    public function getDateCreated() 
    { 
     return $this->dateCreated; 
    } 

    /** 
    * Set totalValue 
    * 
    * @param decimal $totalValue 
    * @return Budget 
    */ 
    public function setTotalValue($totalValue) 
    { 
     $this->totalValue = $totalValue; 

     return $this; 
    } 

    /** 
    * Get totalValue 
    * 
    * @return decimal 
    */ 
    public function getTotalValue() 
    { 
     return $this->totalValue; 
    } 
} 

我的一對多客戶與預算的關係:

CDG\PanelBundle\Entity\Client: 
    type: entity 
    table: client 
    repositoryClass: CDG\PanelBundle\Entity\ClientRepository 
    id: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
    fields: 
     name: 
      type: string 
      length: 255 
     phone: 
      type: string 
      length: 255 
     email: 
      type: string 
      length: 255 
     streetName: 
      type: string 
      length: 255 
      column: street_name 
     district: 
      type: string 
      length: 255 
     number: 
      type: string 
      length: 255 
     city: 
      type: string 
      length: 255 
     zipCode: 
      type: string 
      length: 255 
      column: zip_code 
    oneToMany: 
     budget: 
      targetEntity: Budget 
      mappedBy: clientId 
    lifecycleCallbacks: { } 

而且隨着經濟的我多對一關係客戶:

CDG\PanelBundle\Entity\Budget: 
    type: entity 
    table: budget 
    repositoryClass: CDG\PanelBundle\Entity\BudgetRepository 
    id: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
    fields: 
     address: 
      type: string 
      length: 255 
     installments: 
      type: integer 
     checkDays: 
      type: integer 
      column: check_days 
     dateStart: 
      type: datetime 
      column: date_start 
     dateCreated: 
      type: datetime 
      column: date_created 
     totalValue: 
      type: decimal 
      column: total_value 
      nullable: true 
    manyToOne: 
     clientId: 
      targetEntity: Client 
      inversedBy: budget 
      joinColumn: 
       name: client_id 
       referencedColumnName: id 
    lifecycleCallbacks: { } 

要完成,我的addAction,從BudgetController

public function addAction(Request $request) 
{ 
    $form = $this->createForm(new BudgetType()); 

    if ($request->getMethod() == 'POST') { 
     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $manager = $this->getDoctrine()->getManager(); 
      $manager->persist($form->getData()); 
      $manager->flush(); 

      $this->addFlash('success', 'Novo orçamento adicionado'); 

      return $this->redirect($this->generateUrl('panel_budgets')); 
     } 
    } 

    return $this->render('PanelBundle:Budget:add.html.twig', array(
     'clients' => $manager->getRepository('PanelBundle:Client')->findAll(), 
     'form' => $form->createView() 
    )); 
} 

當提交表單,則拋出異常:

警告:spl_object_hash()預計參數1是對象,給出的整數

public function getEntityState($entity, $assume = null) 
{ 
    $oid = spl_object_hash($entity); 
    if (isset($this->entityStates[$oid])) { 
     return $this->entityStates[$oid]; 

它的堆棧跟蹤:

at ErrorHandler ->handleError ('2', 'spl_object_hash() expects parameter 1 to be object, integer given', '/home/gabriel/Documents/CasaDoGesso/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php', '1389', array('entity' => '2', 'assume' => '2')) 
at spl_object_hash ('2') 
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 1389 + 
at UnitOfWork ->getEntityState ('2', '2') 
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 788 + 
at UnitOfWork ->computeAssociationChanges (array('fieldName' => 'clientId', 'targetEntity' => 'CDG\PanelBundle\Entity\Client', 'inversedBy' => 'budget', 'joinColumns' => array(array('referencedColumnName' => 'id', 'name' => 'client_id')), 'type' => '2', 'mappedBy' => null, 'isOwningSide' => true, 'sourceEntity' => 'CDG\PanelBundle\Entity\Budget', 'fetch' => '2', 'cascade' => array(), 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false, 'sourceToTargetKeyColumns' => array('client_id' => 'id'), 'joinColumnFieldNames' => array('client_id' => 'client_id'), 'targetToSourceKeyColumns' => array('id' => 'client_id'), 'orphanRemoval' => false), '2') 
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 687 + 
at UnitOfWork ->computeChangeSet (object(ClassMetadata), object(Budget)) 
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 404 + 
at UnitOfWork ->computeScheduleInsertsChangeSets() 
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 711 + 
at UnitOfWork ->computeChangeSets() 
in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php at line 297 + 
at UnitOfWork ->commit (null) 
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 340 + 
at EntityManager ->flush() 
in src/CDG/PanelBundle/Controller/BudgetController.php at line 36 + 
at BudgetController ->addAction (object(Request)) 
at call_user_func_array (array(object(BudgetController), 'addAction'), array(object(Request))) 
in app/bootstrap.php.cache at line 3109 + 
at HttpKernel ->handleRaw (object(Request), '1') 
in app/bootstrap.php.cache at line 3071 + 
at HttpKernel ->handle (object(Request), '1', true) 
in app/bootstrap.php.cache at line 3222 + 
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in app/bootstrap.php.cache at line 2444 + 
at Kernel ->handle (object(Request)) 
in web/app_dev.php at line 28 + 

這並不意味着任何東西給我。我不知道該怎麼辦。其他的解決方案根本沒有幫助我。如果你需要更多的代碼,請問我,我會編輯它。謝謝。

回答

3

我建議你在你的實體使用註釋,並且開始使用formbuilder。還要向客戶實體添加一個__toString()方法!

您的實體:

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\Common\Collections\ArrayCollection; 

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

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=64) 
    */ 
    private $name; 

    // YOUR OTHER CLIENT FIELDS 

    /** 
    * @ORM\OneToMany(targetEntity="Budget", mappedBy="client") 
    **/ 
    private $budgets; 

    public function __construct() { 
     $this->budgets = new ArrayCollection(); 
    } 

    /* this function is required to present the entity as a string */ 
    public function __toString() { 
     return $this->name; 
    } 

} 

和第二:

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

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

    // YOUR BUDGET FIELDS 

    /** 
    * @ORM\ManyToOne(targetEntity="Client", inversedBy="budgets") 
    * @ORM\JoinColumn(name="client_id", referencedColumnName="id") 
    **/ 
    private $client; 

} 

然後把從項目主目錄下的控制檯命令: (也許你要輸入單詞PHP每一個命令

app/console doctrine:generate:entities AppBundle 
app/console doctrine:schema:update --force 
app/console doctrine:generate:crud AppBundle:Client 
(type YES by the question Do you want to generate the "write" actions [no]?) 
app/console doctrine:generate:crud AppBundle:Budget 
(type YES by the question Do you want to generate the "write" actions [no]?) 
之前)

看一看新路線:

app/console router:debug 

正如你可以看到你可以瀏覽到

網/ app_dev.php /客戶/

web/app_dev。php/budget/並且看看!

然後打開../src/AppBundle/Form/BudgetType.php在編輯器中,你會發現這條規則:

$builder 
     ->add('client') 
    ; 

這是在客戶端屬性添加到窗體。 Symfony自動將此表單字段設置爲entity formtype。你可以這樣寫:

$builder 
    ->add('client', 'entity', array(
     'class' => 'AppBundle:Client', 
     'empty_value' => '', 
    )) 
; 

所以現在你可以在預算表中選擇你的客戶。

正如您可能已經注意到的那樣,這些命令還爲您添加了ClientController和BudgetController。 有關formtypes的更多信息,請參閱文檔。

+0

在我的Client實體中在我的'BudgetType'和'__toString()'方法中添加'entity'字段類型解決了這個問題。我不需要做其他任何事情,比如新的路線和新的Bundle,這會破壞我的項目結構。對於你的建議:我寧願使用YAML代替註釋,因爲對我來說代碼看起來更有組織,即使我必須寫更多。無論如何,謝謝。 – mfgabriel92

+0

很高興聽到你可以解決它。仍然不得不說,你應該使用formbuilder。它最好的就是生成表單。 –

+0

你說這個formbuilder是什麼?自動生成還是其他東西?教義產生形式和東西的方式真是醜陋而混亂。我認爲在生成所有內容之前理解發生的事情非常重要。面對這樣的問題是很好的,甚至有人願意爲你寫出巨大的解決方案。再次感謝你。 :) – mfgabriel92