php - Doctrine 2 @Id @GeneratedValue issue -



php - Doctrine 2 @Id @GeneratedValue issue -

so though have resolved own issue, know if did right way or not?

i'm using code doctrine docs insert new record database code looks , looping on xlsx beingness process phpexcel.

i have set entity

/** * @var integer $id * * @column(name="uploaduserid", type="integer", nullable=false) * @id * @generatedvalue(strategy="identity") */ private $id;

--

$user = new \uploaduserdetails(); $user->id = 0; $user->userid = $item->userid; $user->email = $cells['a']; $user->firstname = $cells['b']; $user->lastname = $cells['c']; $user->street = $cells['d']; $user->city = $cells['e']; $user->state = $cells['f']; $user->zip = $cells['g']; $user->mobile = $cells['h']; $entitymanager->persist($user); $entitymanager->flush();

now, though works , record appears in database (mariadb), bit giving me problem , know seems little on kill

$user->id = 0;

according docs http://docs.doctrine-project.org/en/latest/tutorials/getting-started.html (shown below) should not need set. set name, persist object , flush. in case fatal error below if don't set value of id numeric. result in db unique number expected , not value set, great. i've tried setting generation strategy "auto", leaving out , leaving @ @generatedvalue. generate same error.

fatal error: uncaught exception 'doctrine\orm\ormexception' message 'entity of type uploaduserdetails missing assigned id field 'id'. identifier generation strategy entity requires id field populated before entitymanager#persist() called.

<?php // src/product.php /** * @entity @table(name="products") **/ class product { /** @id @column(type="integer") @generatedvalue **/ protected $id; /** @column(type="string") **/ protected $name; // .. (other code) }

--

<?php // create_product.php require_once "bootstrap.php"; $newproductname = $argv[1]; $product = new product(); $product->setname($newproductname); $entitymanager->persist($product); $entitymanager->flush(); echo "created product id " . $product->getid() . "\n";

found reply own question.

you should utilize @generatedvalue(strategy="identity") on primary key auto generating numbers or sequence.

php doctrine2

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -