0

将我的帖子数据从我的控制器传递到我的映射器以插入我的数据库时,我似乎丢失了它。我是新手,使用留言簿模板,但将其修改为添加“交易”。这就是我所拥有的:

Controller
public function newAction()
    {
      $request = $this->getRequest();

      $form    = new Application_Form_Deal();

      if ($this->getRequest()->isPost()) {

            if ($form->isValid($request->getPost())) {

                $posts = $form->getValues();
                //print_r($posts);
                $newdeal = new Application_Model_Deal($posts);
                $mapper  = new Application_Model_DealMapper();
                $mapper->save($newdeal);
                return $this->_helper->redirector('index');
            }
        }

        $this->view->form = $form;
    }

这是我的映射器

public function save(Application_Model_Deal $deal)
        {

            $data = array(
                'dealname'      => $deal->getDealName(),
                'dealclient'    => $deal->getDealClient(),        
                'dealtelephone' => $deal->getDealTelephone(),
                'dealvalue'     => $deal->getDealValue(),
                'dealdescription' => $deal->getDealDescription(),
                'dealcreated'   => date('Y-m-d H:i:s'),
                'dealmodified'  => date('Y-m-d H:i:s'),
            );

            /*echo "<pre>";
            print_r($data);
            echo "</pre>";
            exit();*/

             if (null === ($dealid = $deal->getDealId())) {
                unset($data['dealid']);
                $this->getDbTable()->insert($data);
            } else {
                $this->getDbTable()->update($data, array('dealid = ?' => $dealid));
            }
        }

protected $_dealmodified;
    protected $_dealcreated;
    protected $_dealdescription;
    protected $_dealvalue;
    protected $_dealtelephone;
    protected $_dealclient;
    protected $_dealname;
    protected $_dealid;

    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid deal property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid deal property');
        }
        return $this->$method($value);
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

     public function setDealModified($ts)
    {
        $this->_dealmodified = $ts;
        return $this;
    }

    public function getDealModified()
    {
        return $this->_dealmodified;
    }

    public function setDealCreated($ts)
    {
        $this->_dealcreated = $ts;
        return $this;
    }

    public function getDealCreated()
    {
        return $this->_dealcreated;
    }

    public function setDealDescription($text)
    {
        $this->_dealdescription = (string) $text;
        return $this;
    }

    public function getDealDescription()
    {
        return $this->_dealdescription;
    }

    public function setDealValue( $text)
    {
        $this->_dealvalue =  $text;
        return $this;
    }

    public function getDealValue()
    {
        return $this->_dealvalue;
    }

    public function setDealTelephone($text)
    {
        $this->_dealtelephone = $text;
        return $this;
    }

    public function getDealTelephone()
    {
        return $this->_dealtelephone;
    }

    public function setDealClient($text)
    {
        $this->_dealclient = $text;
        return $this;
    }

    public function getDealClient()
    {
        return $this->_dealclient;
    }

    public function setDealName($text)
    {
        $this->_dealname = $text;
        return $this;
    }

    public function getDealName()
    {
        return $this->_dealname;
    }

    public function setDealId($dealid)
    {
        $this->_dealid = (int) $dealid;
        return $this;
    }

    public function getDealId()
    {
        // $this->_dealid = (int) $value;
        return $this->_dealid;
    }

我完全不知道为什么,当我在 Mapper 中 print_r 我的 $data var 时,一切都消失了。

请帮忙!

4

2 回答 2

0

save()在映射器中的方法没有您用作参数的第二个$post参数。

重构:

public function save(Application_Model_Deal $deal, array $post)...
于 2012-10-12T13:24:49.993 回答
0

这看起来很奇怪$mapper->save($newdeal, $posts);

你的 api 是save(Application_Model_Deal $deal)

所以它应该是$mapper->save($newdeal);.

如果您真的想掌握映射器和模型,这三个链接对我有很大帮助:

http://survivethedeepend.com/

http://phpmaster.com/building-a-domain-model/

http://phpmaster.com/integrating-the-data-mappers/

您正在追求特定的工作流程:

  • 现在形式
  • 从数据中收集($post)
  • 验证 $post 数据 (if(isValid())
  • 获取有效和过滤的表单值 ($form->getValues())
  • 实例化您的交易对象 ($newdeal = new Application_Model_Deal($posts);)
  • 使用映射器保存/更新您的交易
  • 成功重定向

一旦你的交易对象被创建,$post 数据就没有进一步的价值,交易对象是需要考虑的重要事情。希望这个对你有帮助。

好的,我想我找到了:

您的问题围绕着这一$method = 'set' . ucfirst($key);行将尝试调用$this->setProperty();您的 getter 和 setter 设置骆驼案例,因此它们将无法工作。解决此问题的最简单方法是将您的 getter 和 setter 重命名为:

 public function setDealmodified($ts)//not camel cased only first letter is capped.
    {
        $this->_dealmodified = $ts;
        return $this;
    }

或者你必须对你的数组键进行驼峰式处理。

于 2012-10-12T13:24:52.310 回答