Symfony2Doctrine - 数据库中的存储阵列



我正在构建一个电子商务应用程序,我想存储用户的订单。

仅供参考:我正在使用symfony 2.8Doctrine2

所以我的订单实体中order了一个数组字段。在我的控制器中,我用我想要的所有信息(产品、用户信息、数量、tva)构建一个数组。但是我遇到了SQL错误,我不知道为什么。但我认为这是 symfony2 序列化我的数组的时候。

错误 :

未捕获的 PHP 异常原则\DBAL\Exception\SyntaxErrorException:"使用参数执行'INSERT INTO 订单(验证、日期、引用、订单user_id) 值 (?, ?, ?, ?, ?)' 时发生异常 [0, "2016-04-23 13:07:55", 0, "a:7:{s:3:\"tva\";a:2:{s:3:\"20%\";d:299.8000000000001;s:3:\"10%\";d:21;}s:7:\"product\";a:3:{i:1;a:5:{s:9:\"reference\";s:9:\"PROD00001\";s:4:\"name\";s:10:\"MacBookPro";s:8:\"quantity\";i:1;s:7:\"priceHT\";d:999;s:8:\"priceTTC\";d:1198.8;}i:2;a:5:{s:9:\"reference\";s:9:\"PROD00002\";s:4:\"name\";s:16:\"耳机苹果";s:8:\"quantity";s:1:\"3\";s:7:\"priceHT";d:69.98999999999995;s:8:\"priceTTC\";d:76.989999999999995;}i:3;a:5:{s:9:\"reference\";s:9:\"PROD00003\";s:4:\"name\";s:20:\"T\u00e9l\u00e9vision Samsung\";s:8:\"quantity\";i:1;s:7:\"priceHT\";d:499.99000000000001;s:8:\"priceTTC\";d:599.99000000000001;}}s:8:\"delivery\";a:9:{s:9:\"firstname\";s:3:\"Doe\";s:8:\"lastName\";s:4:\"John\";s:5:\"email\";s:12:\"test@test.fr\";s:5:\"phone\";s:10:\"6666666666\";s:6:\"adress\";s:14:\"10 rue du test\";s:10:\"complement\";N;s:4:\"city\";s:6:\"Nantes\";s:7:\"country\";s:6:\"France\";s:10:\"postalCode\";s:5:\"44000\";}s:6:\"invoic\";a:9:{s:9:\"firstname\";s:3:\"Doe\";s:8:\"lastName\";s:4:\"John\";s:5:\"email\";s:12:\"test@test.fr\";s:5:\"phone\";s:10:\"6666666666\";s:6:\"adress\";s:14:\"10 rue du test\";s:10:\"complement\";N;s:4:\"city\";s:6:\"Nantes\";s:7:\"country\";s:6:\"France\";s:10:\"postalCode\";s:5:\"44000\";}s:8:\"amountHT\";d:1709;s:9:\"amountTTC\";d:2030;s:5:\"token\";s:40:\"2627ca31ef4d9d6ab3bf69dd38f02cbb8468bf7f\";}", 2]: SQLSTATE[42000]: 语法错误或访问冲突:1064 SQL 语法有错误;检查与您的MySQL服务器版本相对应的手册,了解在'order, user_id)附近使用的正确语法 值 (0, '2016-04-23 13:07:55', 0, 'a:7:{s:3:\"tva\";a:2:{s:3:' 在第 1 行" 在 C:\wamp\www\eCommerce\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php第 90 行

这是我的订单实体

/**
  * Orders
  *
  * @ORMTable(name="orders")
  * @ORMEntity(repositoryClass="EcommerceEcommerceBundleRepositoryOrdersRepository")
*/
class Orders
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var bool
 *
 * @ORMColumn(name="validate", type="boolean")
 */
private $validate;
/**
 * @var DateTime
 *
 * @ORMColumn(name="date", type="datetimetz")
 */
private $date;
/**
 * @var int
 *
 * @ORMColumn(name="reference", type="integer")
 */
private $reference;
/**
 * @ORMManyToOne(targetEntity="UsersUsersBundleEntityUsers", inversedBy="orders")
 * @ORMJoinColumn(nullable=true)
 */
private $user ;
/**
 * @var array
 *
 * @ORMColumn(name="order", type="array")
 */
private $order;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * Set validate
 *
 * @param boolean $validate
 * @return Orders
 */
public function setValidate($validate)
{
    $this->validate = $validate;
    return $this;
}
/**
 * Get validate
 *
 * @return boolean 
 */
public function getValidate()
{
    return $this->validate;
}
/**
 * Set date
 *
 * @param DateTime $date
 * @return Orders
 */
public function setDate($date)
{
    $this->date = $date;
    return $this;
}
/**
 * Get date
 *
 * @return DateTime 
 */
public function getDate()
{
    return $this->date;
}
/**
 * Set reference
 *
 * @param integer $reference
 * @return Orders
 */
public function setReference($reference)
{
    $this->reference = $reference;
    return $this;
}
/**
 * Get reference
 *
 * @return integer 
 */
public function getReference()
{
    return $this->reference;
}
/**
 * Set user
 *
 * @param UsersUsersBundleEntityUsers $user
 * @return Orders
 */
public function setUser(UsersUsersBundleEntityUsers $user = null)
{
    $this->user = $user;
    return $this;
}
/**
 * Get user
 *
 * @return UsersUsersBundleEntityUsers
 */
public function getUser()
{
    return $this->user;
}
/**
 * Constructor
 */
public function __construct()
{
    $this->products = new DoctrineCommonCollectionsArrayCollection();
}
/**
 * Add products
 *
 * @param EcommerceEcommerceBundleEntityProducts $products
 * @return Orders
 */
public function addProduct(EcommerceEcommerceBundleEntityProducts $products)
{
    $this->products[] = $products;
    return $this;
}

/**
 * Set order
 *
 * @param array $order
 * @return Orders
 */
public function setOrder($order)
{
    $this->order = $order;
    return $this;
}
/**
 * Get order
 *
 * @return array 
 */
public function getOrder()
{
    return $this->order;
}
}

我的控制器

public function orderAction(Request $request){
    $session = $request->getSession() ;
    $em = $this->getDoctrine()->getManager() ;
    $generator = $this->container->get('security.secure_random');
    if(!$session->has('order')){
        $order = new Orders();
    } else {
        $order = $em->getRepository('EcommerceBundle:Orders')->find($session->get('order')) ;
    }
    $order->setDate(new DateTime());
    $order->setUser($this->container->get('security.context')->getToken()->getUser());
    $order->setValidate(0);
    $order->setReference(bin2hex($generator->nextBytes(20)));
    $order->setOrder($this->order($request));
    if(!$session->has('order')){
        $em->persist($order);
        $session->set('order',$order) ;
    }
    $em->flush() ;
    return new Response($order->getId());
}

我现在真的被困住了

告诉我您是否需要更多信息或代码。

谢谢。

order是一个保留的SQL字。您需要引用它:引用保留字

相关内容

  • 没有找到相关文章

最新更新