eXe
eXe

Reputation: 660

Doctrine 2 Entity Column Method Name

Is there a way to define the method name manuall instead of the generated ones?

Lets show an example on the following entity (with auto generated methods from doctrine):

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * 
 * @ORM\Entity(repositoryClass="Application\EntityRepository\Zone")
 * @ORM\Table(name="zones")
 *
 */
class Zone
{
    /**
     * @var ZoneRecordNS[]
     * @ORM\OneToMany(targetEntity="Application\Entity\ZoneRecordNS", mappedBy="zone", cascade={"all"})
     * @ORM\OrderBy({"ownername" = "ASC"})
     */
    protected $recordsns;

    /**
     * Get recordsns
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getRecordsns()
    {
        return $this->recordsns;
    }

    /**
     * Add recordsn
     *
     * @param \Application\Entity\ZoneRecordNS $recordsn
     *
     * @return Zone
     */
    public function addRecordsn(\Application\Entity\ZoneRecordNS $recordsn)
    {
        $this->recordsns[] = $recordsn;

        return $this;
    }

    /**
     * Remove recordsn
     *
     * @param \Application\Entity\ZoneRecordNS $recordsn
     */
    public function removeRecordsn(\Application\Entity\ZoneRecordNS $recordsn)
    {
        $this->recordsns->removeElement($recordsn);
    }
}

Doctrine 2 writes the method "addRecordsn", but i would like to have it named "addRecordNS".

If i rewrite the autogenerated methods and force a "orm:generate-entities --generate-methods=1", doctrine will recreate the the autogenerated methods again..

Is it possible to create a annotation, or something other, to force doctrine to use the user-written methods instead of the auto generated ones?

Update i tried renaming also now.

with "$recordNS":

/**
 * @var ZoneRecordNS[]
 * @ORM\OneToMany(targetEntity="Application\Entity\ZoneRecordNS", mappedBy="zone", cascade={"all"})
 * @ORM\OrderBy({"ownername" = "ASC"})
 */
protected $recordNS;

/**
 * Add recordN
 *
 * @param \Application\Entity\ZoneRecordNS $recordN
 *
 * @return Zone
 */
public function addRecordN(\Application\Entity\ZoneRecordNS $recordN)
{
    $this->recordNS[] = $recordN;

    return $this;
}

/**
 * Remove recordN
 *
 * @param \Application\Entity\ZoneRecordNS $recordN
 */
public function removeRecordN(\Application\Entity\ZoneRecordNS $recordN)
{
    $this->recordNS->removeElement($recordN);
}

/**
 * Get recordNS
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getRecordNS()
{
    return $this->recordNS;
}

and with "$recordNSs":

/**
 * @var ZoneRecordNS[]
 * @ORM\OneToMany(targetEntity="Application\Entity\ZoneRecordNS", mappedBy="zone", cascade={"all"})
 * @ORM\OrderBy({"ownername" = "ASC"})
 */
protected $recordNSs;

/**
 * Add recordNSs
 *
 * @param \Application\Entity\ZoneRecordNS $recordNSs
 *
 * @return Zone
 */
public function addRecordNSs(\Application\Entity\ZoneRecordNS $recordNSs)
{
    $this->recordNSs[] = $recordNSs;

    return $this;
}

/**
 * Remove recordNSs
 *
 * @param \Application\Entity\ZoneRecordNS $recordNSs
 */
public function removeRecordNSs(\Application\Entity\ZoneRecordNS $recordNSs)
{
    $this->recordNSs->removeElement($recordNSs);
}

/**
 * Get recordNSs
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getRecordNSs()
{
    return $this->recordNSs;
}

so it seems not possible in my variable name delaration to get correct human readable method names :-(.

sadly there is also no annotation to force a manual typed method name.

Upvotes: 2

Views: 452

Answers (1)

Wilt
Wilt

Reputation: 44373

Doctrine uses a singularize method to get the method names from your property name. So the property name is a base for the method names. So if you want NS with capitalized characters you should also write your property name like that. The additional 's' makes it plural and is stripped for a single element. This is why you get addRecordsn and not what you want addRecordns (check the order of sand n)

protected $recordNSs;

will give you addRecordNS and removeRecordNS

or use

protected $records;

which will give you addRecord and removeRecord

or use

protected $NSRecords;

which will give you addNSRecord and removeNSRecord

Upvotes: 1

Related Questions