0
class Accounts extends AbstractType
{
    private $em;

    private $accountrepo = null;

    private $choices = array();

    public function __construct()
    {
    }

    public function setAccountRepository($repo)
    {
        $this->accountrepo = $repo;
        return $this;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
        'choices' => $this->getChoices(),
        'attr' => [
                    'data-dojo-type' => 'dijit/form/FilteringSelect',
                    ],
            'data_class' => 'Far\MT\AccountBundle\Entity\Account'
        ]);
    }

    public function getParent()
    {
        return 'choice';
    }

    public function getName()
    {
        return 'Accounts';
    }

    public function getChoices()
    {
        if ( count($this->choices) > 0 ) {
            return $this->choices;
        }
        $this->choices[0] = "";
        foreach ($this->accountrepo->findAll() as $account) {
            $this->choices[$account->getId()] = $account->getName() . " (" .
            $account->getCurrency().") ";
        }
        return $this->choices;
    }

}

上記のコードを使用すると、次のエラーが表示されます。

注意: クラス Proxies__CG__\Far\MT\AccountBundle\Entity\Account のオブジェクトは、/workspace/tools/vendorsymfony2/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList で int に変換できませんでした。 php行457

これは、レコードを編集するときにのみ発生します。データベース値をフィルタリングして「コンボボックス」に配置するために、ChoiceList タイプを持つこのタイプを使用しています。

私が間違っている可能性があることについての提案はありますか?

編集

use Doctrine\ORM\Mapping as ORM;

/**
 * Movement
 *
 * @ORM\Entity(repositoryClass="Far\MT\AccountBundle\Entity\MovementRepository")
 * @ORM\Table(name="movement")
 */
class Movement
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \Date
     *
     * @ORM\Column(name="date", type="date")
     */
    private $date;

    /**
     * @var string
     *
     * @ORM\ManyToOne(targetEntity="MovementType")
     * @ORM\JoinColumn(name="movementtype", referencedColumnName="id", unique=false)
     */
    private $Type;

    /**
     * @var float
     *
     * @ORM\Column(name="value", type="float")
     */
    private $value;

    /**
     * @var Account
     *
     * @ORM\ManyToOne(targetEntity="Account", cascade={"persist"})
     * @ORM\JoinColumn(name="fromaccount", referencedColumnName="id", unique=false)
     */
    private $fromAccount;

    /**
     * @var Account
     *
     * @ORM\ManyToOne(targetEntity="Account", cascade={"persist"})
     * @ORM\JoinColumn(name="toaccount", referencedColumnName="id", unique=false)
     */
    private $toAccount;

    /**
     * @var string
     *
     * @ORM\ManyToOne(targetEntity="ExpenseType", cascade={"persist"})
     * @ORM\JoinColumn(name="expensetype", referencedColumnName="id",
     * nullable=true, unique=false)
     */
    private $ExpenseType;

    /**
     * @var string
     *
     * @ORM\ManyToOne(targetEntity="Holder",  cascade={"persist"})
     * @ORM\JoinColumn(name="holder", referencedColumnName="id", unique=false)
     */
    private $orderHolder;

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

    /**
     * @var float
     *
     * @ORM\Column(name="xrate", type="float", nullable=true)
     */
    private $xrate = null;

    public function getDescription()
    {
        return $this->description;
    }

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

    /**
     * Set Account
     *
     * @param string $account
     * @return Movement
     */
    public function setAccount($account)
    {
        $this->Account = $account;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set value
     *
     * @param float $value
     * @return Movement
     */
    public function setValue($value)
    {
        $this->value = $value;

        return $this;
    }

    /**
     * Get value
     *
     * @return float
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Set fromAccount
     *
     * @param string $fromAccount
     * @return Movement
     */
    public function setFromAccount($fromAccount)
    {
        $this->fromAccount = $fromAccount;

        return $this;
    }

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

    /**
     * Set toAccount
     *
     * @param string $toAccount
     * @return Movement
     */
    public function setToAccount($toAccount)
    {
        $this->toAccount = $toAccount;

        return $this;
    }

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

    /**
     * Set ExpenseType
     *
     * @param string $expenseType
     * @return Movement
     */
    public function setExpenseType($expenseType)
    {
        $this->ExpenseType = $expenseType;

        return $this;
    }

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

    /**
     * Set orderHolder
     *
     * @param string $orderHolder
     * @return Movement
     */
    public function setOrderHolder($orderHolder)
    {
        $this->orderHolder = $orderHolder;

        return $this;
    }

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

    /**
     * Set exchange rate for movement
     *
     * @param float $xrate
     */
    public function setXrate($xrate)
    {
        $this->xrate = (double)$xrate;
        return $this;
    }

    /**
     * Return movement exchange rate
     *
     * @return float
     */
    public function getXrate()
    {
        return $this->xrate;
    }
}

エンティティ アカウント

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

/**
 * Account
 *
 * @ORM\Table(name="account")
 *
 * @ORM\Entity(repositoryClass="Far\MT\AccountBundle\Entity\AccountRepository")
 */
class Account
{

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

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

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

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

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

    /**
     * @var Far\MT\AccountBundle\Entity\Holder
     *
     * @ORM\ManyToMany(targetEntity="Holder", inversedBy="Accounts")
     * @ORM\JoinTable(name="account_holder",
     * joinColumns={@ORM\JoinColumn(name="account_id", referencedColumnName="id")},
     * inverseJoinColumns={@ORM\JoinColumn(name="holder_id",
     * referencedColumnName="id")}
     * )
     *
     */
    private $holders;

    /**
     * @var Far\MT\AccountBundle\Entity\Account
     *
     * @ORM\OneToMany(targetEntity="Account", mappedBy="parentAccount")
     */
    private $pocketAccounts;
    /**
     *
     * @ORM\ManyToOne(targetEntity="Account", inversedBy="pocketAccounts", cascade={"persist"})
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parentAccount;

    /**
     * Specifies the account type has string
     *
     * @var String
     *
     * @ORM\Column(name="accounttype", type="string", length=40)
     */
    private $accountType;

    /**
     * Total value of available money this should not account money that is
     * pending transaction
     *
     * @var double
     *
     * @ORM\Column(name="totalavailable", type="float")
     */
    private $totalAvailable = 0;

    /**
     * Total value of accounted money this takes into account money that has
     * pending transaction
     *
     * @var double
     *
     * @ORM\Column(name="totalaccounted", type="float")
     */
    private $totalAccounted = 0;

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

    /**
     * Set code
     *
     * @param string $code
     * @return Account
     */
    public function setCode($code)
    {
        $this->code = str_replace(" ", "", strtoupper(trim($code)));

        return $this;
    }

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

    /**
     * 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 currency
     *
     * @param string $currency
     * @return Account
     */
    public function setCurrency($currency)
    {
        $this->currency = $currency;

        return $this;
    }

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

    /**
     * Set holders
     *
     * @param null|Doctrine\Common\Collections\ArrayCollection $holders
     * @return Account
     */
    public function setHolders($holders)
    {
        if ($holders === null) {
            $this->holders = new ArrayCollection();
        } else {
            $this->holders = $holders;
        }

        return $this;
    }

    /**
     * Get holders
     *
     * @return string
     */
    public function getHolders()
    {
        return $this->holders;
    }
    /**
     *
     * Get Pocket accounts
     *
     * @return \Doctrine\Common\Collections\ArrayCollection()
     */
    public function getPocketAccounts()
    {
        return $this->pocketAccounts;
    }

    public function setPocketAccounts($PocketAccounts)
    {
        $this->pocketAccounts = $PocketAccounts;
        return $this;
    }

    /**
     * Get Parent Account
     *
     * @return \Far\MT\AccountBundle\Entity\Account
     *
     */
    public function getParentAccount()
    {
        return $this->parentAccount;
    }

    public function setParentAccount($ParentAccount)
    {
        $this->parentAccount = ($ParentAccount);
        return $this;
    }
    /**
     * does this account has a parent
     *
     * @return boolean
     */
    public function hasParentAccount()
    {
        return ($this->parentAccount !== null);
    }

    public function setAccountType($accountType)
    {
        $this->accountType = $accountType;
        return $this;
    }

    public function getAccountType()
    {
        return $this->accountType;
    }

    public function getTotalAccounted()
    {
        return $this->totalAccounted;
    }

    public function setTotalAccounted($total)
    {
        $this->totalAccounted = $total;
        return $this;
    }

    public function getTotalAvailable()
    {
        return $this->totalAvailable;
    }

    public function setTotalAvailable($total)
    {
        $this->totalAvailable = $total;
        return $this;
    }

    public function __toString()
    {

        return 'Account';
    }
}

更新されるのは、fromAccount と toAccount にアカウントを含む Movement エンティティであることに注意してください。

4

1 に答える 1

0

彼が私に指摘した答えを知らなかったので、私は data_class パラメータを理解していませんでした。

    'data_class' => 'Far\MT\AccountBundle\Entity\Account'

18:37 < ocramius> netcrash: いいえ、申し訳ありませんが、data_class が何をするかわからないので、どうしようもありません (sf2 フォームのユーザーではありません)。

Data Transformer、data_class がありませんでした (Entity を指定すると、理由がわかりません...)、DataTransformer を配置して問題を解決しました。下記をご覧ください。

use Far\MT\AccountBundle\Form\DataTransformer\AccountToIdTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class Accounts extends AbstractType
{
    /**
     *
     * @var EntityManager
     */
    private $em;

    private $accountrepo = null;

    private $choices = array();

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $transformer = new AccountToIdTransformer($this->em);
        $builder->addModelTransformer($transformer);
    }

    public function setAccountRepository($repo)
    {
        $this->accountrepo = $repo;
        return $this;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
        'choices' => $this->getChoices(),
        'attr' => [
                    'data-dojo-type' => 'dijit/form/FilteringSelect',
                    ]
        ]);
    }

    public function getParent()
    {
        return 'choice';
    }

    public function getName()
    {
        return 'Accounts';
    }

    public function getChoices()
    {
        if ( count($this->choices) > 0 ) {
            return $this->choices;
        }
        $this->choices[0] = "";
        foreach ($this->accountrepo->findAll() as $account) {
            $this->choices[$account->getId()] = $account->getName() . " (" .
            $account->getCurrency().") ";
        }
        return $this->choices;
    }

}

そしてデータトランスフォーマー:

/**
 * This is a transformer for the Account AbstractType
 *
 * @link http://symfony.com/doc/master/cookbook/form/data_transformers.html
 *
 * @author andref
 */
class AccountToIdTransformer implements DataTransformerInterface
{
    /**
     * @var ObjectManager
     */
    private $om;

    /**
     *
     * @param ObjectManager $om
     */
    public function __construct($om)
    {
        $this->om = $om;
    }
    /**
     * Transforms an object (issue) to a int (Id).
     *
     * @param  Account|null $account
     * @return string
     */
    public function transform($account)
    {
        if (null === $account) {
            return "";
        }

        return $account->getId();
    }

    /**
     * Transforms a int (Id) to an object (Account).
     *
     * @param  int $Id
     *
     * @return Account|null
     *
     * @throws TransformationFailedException if object (Account) is not found.
     */
    public function reverseTransform($id)
    {
        if (!$id) {
            return null;
        }

        $Account = $this->om
            ->getRepository('FarMTAccountBundle:Account')
            ->findOneBy(array('id' => $id))
        ;

        if (null === $Account) {
            throw new TransformationFailedException(sprintf(
                'An Account with Id "%s" does not exist!',
                $id
            ));
        }

        return $Account;
    }
}
于 2013-04-13T23:18:19.877 に答える