0

I am trying to create a saveAction in zend2 framework using doctrine.

in my PromotionsController i have this action:

public function saveLinkAction() {
    $view = new ViewModel();

    $salonId = (int) $this->params()->fromPost('salon_id', null);


    $addLink = $this->getServiceLocator()->get('Promotions\Model\Link');

        $linkData['salon_id'] = $salonId;
        $linkData['link'] = '/link/example';

        $addLink->setData($linkData);        


    return $view;
}

This is just for learning how to write data in database.

$addLink = $this->getServiceLocator()->get('Promotions\Model\Link');

This line of code is showing an error and i don't know what is the cause?

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Promotions\Model\Link

I have created a Link.php in Model directory.

<?php
namespace Link\Model;

use Application\Model\Entity;
use Zend\Form\Annotation;

/**
 * @Entity
 * @Table(name="promo_link")
 */
class Link extends Entity {

    /********** PROPERTIES **********/

    /**
     * @Id @GeneratedValue(strategy="AUTO") @Column(name="id", type="integer")
     * @var int
     * 
     * @Annotation\Exclude()
     */
    protected $id;

    /**
     * @Column(name="salon", type="integer")
     * @var int 
     * 
     * @Annotation\Options({"label":"Salon"})
     * @Annotation\Validator({"name": "Digits"})
     */
    protected $salon;

    /**
     * @Column(name="link", type="string")
     * @var string 
     */
    protected $link;

     /**
     * @Column(name="start_date", type="string")
     * @var string 
     */
    protected $start_date;

     /**
     * @Column(name="end_date", type="string")
     * @var string 
     */
    protected $end_date;


}
?>
4

2 回答 2

0

该错误告诉您问题出在哪里:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Promotions\Model\Link

含义:ServiceManager 不知道Promotions\Model\Link应该是什么。此键在您的 SMConfig 中不存在,或者在创建要返回的实例时发生一些错误。

TL/DR - 检查有关密钥的 ServiceManager 配置Promotions\Model\Link

于 2013-10-28T10:27:51.363 回答
0

为了将数据保存在数据库中,您将需要 entitymanager。

$link = new Link();
$link->setSalonId($salonId);
$link->setLink('/link/example');

$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$em->persist($link);
$em->flush();

你可以从上面的代码开始。

但是,最好创建一个存储库和一个服务层。该服务应该有权访问 entityManager 并保存您的逻辑。您的控制器应该有权访问此服务。

于 2013-10-30T12:23:55.047 回答