douyu9012 2013-11-03 06:47
浏览 55
已采纳

生成Getter和setter Symfony2

I firstly created the database and then generated the entities from it. After that I generated the getters and setters in the entities using:

php app/console doctrine:generate:entities DigitalManager

Later on I decided to create the association (One to Many) between the entities, so I added the respective commands in my Entities and what I got is this:

Party Entity:

<?php

namespace DigitalManager\Bundle\ERPBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Party
*
* @ORM\Table(name="party")
* @ORM\Entity
*/
class Party
{ 
/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=45, nullable=true)
 */
private $name;

/**
 * @ORM\ManyToOne(targetEntity="Account", inversedBy="parties")
 * @ORM\JoinColumn(name="account_id", referencedColumnName="aid")
 */
public $account;

/**
 * @var string
 *
 * @ORM\Column(name="address", type="string", length=45, nullable=true)
 */
private $address;

/**
 * @var string
 *
 * @ORM\Column(name="phone_fax", type="string", length=45, nullable=true)
 */
private $phoneFax;

/**
 * @var string
 *
 * @ORM\Column(name="fax", type="string", length=45, nullable=true)
 */
private $fax;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=45, nullable=true)
 */
private $email;

/**
 * @var string
 *
 * @ORM\Column(name="mobile", type="string", length=45, nullable=true)
 */
private $mobile;

/**
 * @var string
 *
 * @ORM\Column(name="country", type="string", length=45, nullable=true)
 */
private $country;

/**
 * @var string
 *
 * @ORM\Column(name="city", type="string", length=45, nullable=true)
 */
private $city;

/**
 * @var string
 *
 * @ORM\Column(name="phone_res", type="string", length=45, nullable=true)
 */
private $phoneRes;

/**
 * @var string
 *
 * @ORM\Column(name="remarks", type="string", length=45, nullable=true)
 */
private $remarks;

/**
 * @var boolean
 *
 * @ORM\Column(name="active", type="boolean", nullable=true)
 */
private $active;

/**
 * @var integer
 *
 * @ORM\Column(name="limit", type="integer", nullable=true)
 */
private $limit;

/**
 * @var integer
 *
 * @ORM\Column(name="term", type="integer", nullable=true)
 */
private $term;

/**
 * @var string
 *
 * @ORM\Column(name="officer_id", type="string", length=45, nullable=true)
 */
private $officerId;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="date", type="datetime", nullable=true)
 */
private $date;

/**
 * @var string
 *
 * @ORM\Column(name="cnic", type="string", length=45, nullable=true)
 */
private $cnic;

/**
 * @var string
 *
 * @ORM\Column(name="ntn", type="string", length=45, nullable=true)
 */
private $ntn;

/**
 * @var boolean
 *
 * @ORM\Column(name="L_O", type="boolean", nullable=true)
 */
private $lO;

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

/**
 * @var integer
 *
 * @ORM\Column(name="acc_id", type="integer", nullable=true)
 */
private $accId;

/**
 * @var integer
 *
 * @ORM\Column(name="party_id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $partyId;



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

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set phoneFax
 *
 * @param string $phoneFax
 * @return Party
 */
public function setPhoneFax($phoneFax)
{
    $this->phoneFax = $phoneFax;

    return $this;
}

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

/**
 * Set fax
 *
 * @param string $fax
 * @return Party
 */
public function setFax($fax)
{
    $this->fax = $fax;

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set mobile
 *
 * @param string $mobile
 * @return Party
 */
public function setMobile($mobile)
{
    $this->mobile = $mobile;

    return $this;
}

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

/**
 * Set country
 *
 * @param string $country
 * @return Party
 */
public function setCountry($country)
{
    $this->country = $country;

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set phoneRes
 *
 * @param string $phoneRes
 * @return Party
 */
public function setPhoneRes($phoneRes)
{
    $this->phoneRes = $phoneRes;

    return $this;
}

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

/**
 * Set remarks
 *
 * @param string $remarks
 * @return Party
 */
public function setRemarks($remarks)
{
    $this->remarks = $remarks;

    return $this;
}

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

/**
 * Set active
 *
 * @param boolean $active
 * @return Party
 */
public function setActive($active)
{
    $this->active = $active;

    return $this;
}

/**
 * Get active
 *
 * @return boolean 
 */
public function getActive()
{
    return $this->active;
}

/**
 * Set limit
 *
 * @param integer $limit
 * @return Party
 */
public function setLimit($limit)
{
    $this->limit = $limit;

    return $this;
}

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

/**
 * Set term
 *
 * @param integer $term
 * @return Party
 */
public function setTerm($term)
{
    $this->term = $term;

    return $this;
}

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

/**
 * Set officerId
 *
 * @param string $officerId
 * @return Party
 */
public function setOfficerId($officerId)
{
    $this->officerId = $officerId;

    return $this;
}

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

/**
 * Set date
 *
 * @param \DateTime $date
 * @return Party
 */
public function setDate($date)
{
    $this->date = $date;

    return $this;
}

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

/**
 * Set cnic
 *
 * @param string $cnic
 * @return Party
 */
public function setCnic($cnic)
{
    $this->cnic = $cnic;

    return $this;
}

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

/**
 * Set ntn
 *
 * @param string $ntn
 * @return Party
 */
public function setNtn($ntn)
{
    $this->ntn = $ntn;

    return $this;
}

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

/**
 * Set lO
 *
 * @param boolean $lO
 * @return Party
 */
public function setLO($lO)
{
    $this->lO = $lO;

    return $this;
}

/**
 * Get lO
 *
 * @return boolean 
 */
public function getLO()
{
    return $this->lO;
}

/**
 * Set type
 *
 * @param string $type
 * @return Party
 */
public function setType($type)
{
    $this->type = $type;

    return $this;
}

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

/**
 * Set accId
 *
 * @param integer $accId
 * @return Party
 */
public function setAccId($accId)
{
    $this->accId = $accId;

    return $this;
}

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

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

Account Entity

<?php

namespace DigitalManager\Bundle\ERPBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Account
 *
 * @ORM\Table(name="account")
 * @ORM\Entity
 */
class Account
{

    public function __construct()
    {
        parent::__construct();

        // The line below must be here as a single category is to be mapped to many products, so an ArrayCollection is required instead of Array.
        $this->parties = new ArrayCollection();
    }

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

    // The metadata above the $products property of the Category object is less important, and simply tells
    // Doctrine to look at the Product.category property to figure out how the relationship is mapped.

    /**
     * @ORM\OneToMany(targetEntity="Party", mappedBy="account")
     */
    protected $parties;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=200, nullable=true)
     */
    private $address;

    /**
     * @var string
     *
     * @ORM\Column(name="level1", type="string", length=45, nullable=true)
     */
    private $level1;

    /**
     * @var string
     *
     * @ORM\Column(name="level2", type="string", length=45, nullable=true)
     */
    private $level2;

    /**
     * @var string
     *
     * @ORM\Column(name="level3", type="string", length=45, nullable=true)
     */
    private $level3;

    /**
     * @var string
     *
     * @ORM\Column(name="contact", type="string", length=45, nullable=true)
     */
    private $contact;

    /**
     * @var integer
     *
     * @ORM\Column(name="aid", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $aid;



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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set level1
     *
     * @param string $level1
     * @return Account
     */
    public function setLevel1($level1)
    {
        $this->level1 = $level1;

        return $this;
    }

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

    /**
     * Set level2
     *
     * @param string $level2
     * @return Account
     */
    public function setLevel2($level2)
    {
        $this->level2 = $level2;

        return $this;
    }

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

    /**
     * Set level3
     *
     * @param string $level3
     * @return Account
     */
    public function setLevel3($level3)
    {
        $this->level3 = $level3;

        return $this;
    }

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

    /**
     * Set contact
     *
     * @param string $contact
     * @return Account
     */
    public function setContact($contact)
    {
        $this->contact = $contact;

        return $this;
    }

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

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

After adding these association commands, I tried to generate the getters and setters for these using:

php app/console doctrine:generate:entities DigitalManager

But it doesn't seem to generate the getters and setters for these associated attributes. I have looked up the syntax through the Symfony2 documentation and it was the same for creating associations. Can anyone please tell me what's wrong here, why does it not generate the getters and setters, although the attributes are there?

  • 写回答

2条回答 默认 最新

  • dstk51319 2013-11-06 09:36
    关注

    There are two process to generate an entity and create a database and so on process.

    I think you can do this way

    1. create a entity using command 
    php app/console doctrine:generate:entity 
    you provide shortcut name Like AcmeHelloBundle. After that it will ask you all the filed name of a table. step by step you create the table.
    
    2. If you not create your database then set database configuration file and run this command
        php app/console doctrine:database:create
    
    3. After create the database you need to create the table depending on your entity that create in list 2 and run the folloing command like
    php app/console doctrine:schema:update --force
    
    
    
    4.Now you can create association with other entity. I just show here create entity but you need to create all entity that need for your project. After perfect association you need to run this command
            php app/console doctrine:generate:entities DigitalManager
    
    5. Finnaly you need to perform this command again
    php app/console doctrine:schema:update --force
    

    This may work fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换