学说 - 通过Symfony3

时间:2017-09-19 11:08:37

标签: php symfony doctrine

我有表Type和Table lignePanier。 lignePanier表中的列type_id是一个外键,它引用了lignePanier表中的id。

我设置的关系是一种类型可能有很多lignesPanier。我已经创建了两个表的实体。

另外,我有Table panier,可能有很多lignesPanier。

我的对象是将现有类型添加到现有的lignePanier 并将所有内容保存在数据库中

这是我的类型实体:

 /**
 * One type has Many lignesPanier.
 * @ORM\OneToMany(targetEntity="EK\EcommerceBundle\Entity\lignePanier", mappedBy="type" , cascade={"persist"} )
 */

private $lignesPanier;

这是我的lignePanier实体:`

/**
 * Many lignes panier have One type.
 * @ORM\ManyToOne(targetEntity="EK\PlateformeBundle\Entity\Type", 
 inversedBy="lignesPanier" )
 * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
 */

private $type;`

这是Panier实体:

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

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


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

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

/**
* @ORM\OneToMany(targetEntity="EK\EcommerceBundle\Entity\lignePanier", mappedBy="panier" , cascade={"persist"})
*/
private $lignesPanier; 

这里我想在数据库中保存所有内容:

$user = $this->getUser();
$panier = $user->getPanier();

if ($this->get('session')->get('vehicule') != null )
{
    $type = $this->get('session')->get('vehicule');
    $lignePanier->setType($type);
}

$lignePanier->setPanier($panier);
$lignePanier->setPiece($piece);
$lignePanier->setPrix($prix);

$panier->addLignePanier($lignePanier);
$panier->setNumArticles($panier->getNumArticles() + $form->getData()->getQuantite() );


$montant = $panier->getMontant() + $prix ; 
$panier->setMontant($montant);


$em->flush();

我的代码确实没有用,我收到了这个错误:

A new entity was found through the relationship 
'EK\EcommerceBundle\Entity\lignePanier#type' that was not configured to 
cascade persist operations for entity: Essence 1.6 i 90 cv Boite 
automatique. To solve this issue: Either explicitly call 
EntityManager#persist() on this unknown entity or configure cascade persist 
this association in the mapping for example @ManyToOne(..,cascade=
{"persist"}).

1 个答案:

答案 0 :(得分:-2)

我尝试了这段代码并且它有效,但是我不明白为什么:

 if(Build.VERSION.SDK_INT >= 21 ) {

        try {
            codec = MediaCodec.createByCodecName("OMX.google.mp3.decoder");

        } catch (IOException e) {
            e.printStackTrace();
        }

        final MediaExtractor extractor = new MediaExtractor();
        try {
            extractor.setDataSource(filePath);     //local file
            Log.i("filePath", String.valueOf(filePath));
            extractor.selectTrack(0);
        } catch (IOException e) {
            e.printStackTrace();
        }


        codec.setCallback(new MediaCodec.Callback() {

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onInputBufferAvailable(MediaCodec mc, int inputBufferId) {

                ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferId);
                // fill inputBuffer with valid data
                int sampleSize = extractor.readSampleData(inputBuffer,0);

                if(extractor.advance) {

                    codec.queueInputBuffer(inputBufferId, 0, sampleSize, 0, 0);


                } else {
                        // EOS
                    codec.queueInputBuffer(inputBufferId, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                    codec.stop();
                    codec.release();
                }


            }
// more callbacks

};


}

`