0

I seem to be losing my post data when passing it from my Controller to my Mapper to insert into my DB. I'm new to this and using the Guestbook template but modified it to be adding a "deal". This is what I have:

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;
    }

This is my Mapper

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;
    }

I am a complete loss as to why, when I print_r my $data var in the Mapper everything is gone.

Please help!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
picotrain
  • 13
  • 4
  • `new Application_Model_Deal($posts)` - can you show the code of the constructor as well here? – raina77ow Oct 12 '12 at 13:04
  • one tip: When using ZF 1.x `Zend_Debug::dump($variable, 'String for title');` is a very useful tool for doing a var_dump as it includes the pre tags and provides a second parameter for a title if dumping several variable at once. I put in on a macro in my IDE, I really miss it in ZF2. – RockyFord Oct 12 '12 at 13:19
  • Constructor ... added it in original post. Thanks for help... :) – picotrain Oct 12 '12 at 13:25
  • var_dump the $newdeal variable in your controller to make sure the object is being constructed properly. This is the piece of data you have to have. Comment out the redirect so you can see the output.. – RockyFord Oct 12 '12 at 13:46
  • After $newdeal I put Zend_Debug::dump($newdeal, 'String for title'); exit(); I get this: String for title object(Application_Model_Deal)#86 (8) { ["_dealmodified:protected"] => NULL ["_dealcreated:protected"] => NULL ["_dealdescription:protected"] => NULL ["_dealvalue:protected"] => NULL ["_dealtelephone:protected"] => NULL ["_dealclient:protected"] => NULL ["_dealname:protected"] => NULL ["_dealid:protected"] => NULL } – picotrain Oct 12 '12 at 13:48
  • ok, no we know your deal object is not being created. That can be fixed. – RockyFord Oct 12 '12 at 13:53

2 Answers2

0

Your save() method in the mapper does not have a second argument which you used as your $post parameter.

Refactor:

public function save(Application_Model_Deal $deal, array $post)...
burntblark
  • 1,680
  • 1
  • 15
  • 25
  • he doesn't need the $post arg as Application_Model_Deal $deal will supply the data. – RockyFord Oct 12 '12 at 13:26
  • YEs sorry, have edited that, was copying an attempt I made to try something else. – picotrain Oct 12 '12 at 13:31
  • @RockyFord Yes I know. But his question is "I seem to be losing my post data..." So I just gave him the reason why his POST data wasn't coming up. May be he has some logic he is trying to achieve – burntblark Oct 12 '12 at 13:33
  • @picotrain Yes. You never collected in the mapper's save method – burntblark Oct 12 '12 at 13:38
  • I don't understand what you mean. COllected the mappers's save method? I've edited the code you saw earlier by the way... – picotrain Oct 12 '12 at 13:40
  • you don't care about your $post data in your mapper. You care that the deal object you created in your controller is present. – RockyFord Oct 12 '12 at 13:42
  • ?-( - You guys are teasing me towards the answer but I am so confused! – picotrain Oct 12 '12 at 13:47
  • @picotrain Sorry for the confusion. I mean't "Yes. You never collected the $post variable in the mapper's save method." And that's why you can't get a dump from it using print_r – burntblark Oct 12 '12 at 13:55
  • Sure, I know sorry for the confusion, have edited that to how it is. It just doesn't make sense to me why it works fine with the Guestbook but not with this. – picotrain Oct 12 '12 at 14:00
  • $data is empty apart from the datetime settings. It just inserts NULL data for the values I am trying to get and get lost. – picotrain Oct 12 '12 at 14:01
0

This looks odd $mapper->save($newdeal, $posts);

your api is save(Application_Model_Deal $deal)

so it should read $mapper->save($newdeal);.

if you really want to get a handle on mappers and models, these three links helped me alot:

http://survivethedeepend.com/

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

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

You are after a specific work flow :

  • present form
  • collect from data (the $post)
  • validate $post data (if(isValid())
  • get valid and filtered form values ($form->getValues())
  • instantiate your deal object ($newdeal = new Application_Model_Deal($posts);)
  • save/update your deal with your mapper
  • on success redirect

once your deal object is created the $post data has no further value and the deal object is the important thing to think about. Hope this helps a bit.

Ok I think I found it:

your problem revolves around this $method = 'set' . ucfirst($key); this line will try and call $this->setProperty(); your getters and setters are set up camel case so they won't work. The easiest way to fix this is to rename your getters and setters to:

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

or you have to camel case your array keys.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • Yes sorry was trying something, the code is indeed $mapper->save($newdeal); as you say – picotrain Oct 12 '12 at 13:28
  • Thanks for the links. Will look one day when my confusion from reading all sorts of stuff has disappeared! – picotrain Oct 12 '12 at 13:41
  • You DA Man! Thanks! Works like a charm, I would have spent a long time trying to figure that out and probably have given up! Thank you so much! – picotrain Oct 12 '12 at 14:04
  • your welcome, sometimes it just takes some time to wrap your head around a problem. – RockyFord Oct 12 '12 at 14:05
  • RockyFord - Any chance I can ask you for a little more help again...it's this thread - http://stackoverflow.com/questions/13034145/zend-method-put – picotrain Oct 25 '12 at 06:24