0

I am trying to save a description field that has varchar(500) in the database. When I look at the Net Panel in firebug the entire description is being posted (200+ words). However, in cakephp only 75 words are being saved. I set a breakpoint in my controller and looked at this->request->data and it has about 150 characters. Below is my code:

<fieldset>
<legend>Create Log</legend>
<?php echo $this->Form->Create('Log', array('inputDefaults' => array('div' => false, 'label' => false))); ?>
    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-folder-open"></i></span>
            <?php echo $this->Form->input('project_id'); ?>
        </div>
    </div>

    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-user"></i></span>
            <input type="text" id="txtName" placeholder="Customer" />
            <?php echo $this->Form->Hidden('customer_id'); ?>
            <a><span class="add-on"><i class="icon-plus" style="color: green;" onclick="window.open('<?php echo $this->Html->Url(array('controller' => 'customers', 'action' => 'add?popup')); ?>', 'Add Customer', 'height=630, width=430')"></i></span></a>
        </div>
    </div>

    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-time"></i></span>
            <?php echo $this->Form->input('time_spent', array('placeholder' => 'Time spent (hrs)')); ?>             
        </div>
    </div> 

    <div class="control-group">
        <div class="input-prepend">
            <span class="add-on"><i class="icon-book"></i></span>
            <?php echo $this->Form->textarea('description', array('placeholder' => 'Description', 'class' => 'logTextArea', 'rows' => '7')); ?>

        </div>
    </div>

<?php echo $this->Form->end(array(
    'label' => 'Save Record',
    'class' => 'btn btn-primary',
    'div' => false
)); ?>

<?php echo $this->Html->script('jquery.autocomplete', array('inline' => false)); ?>

<?php $this->start('script'); ?>
<script type="text/javascript">
    $(document).ready(function () {
        var options, a;
        jQuery(function() {
            options = { 
                serviceUrl: "<?php echo $this->Html->Url(array('controller' => 'logs', 'action' => 'autoComplete.json')); ?>",
                minChars: 2,
                onSelect: function(suggestion){ $('#LogCustomerId').val(suggestion.data); }
            };
            a = $('#txtName').autocomplete(options);
        });
    });
</script>
<?php $this->end(); ?>

Controller:

public function add() {
    $this->set('title_for_layout', 'Add log');

    // Populate projects DDL
    $this->set('projects', $this->Log->Project->find('list', array(
        'order' => array('Project.project_name')
        )));

    if ($this->request->is('post'))
    {
        $this->Log->create();
        if ($this->Log->save($this->request->data))
        {
            $this->Session->setFlash('Log has succesfully been created', 'default', array('class' => 'alert alert-success'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to save log', 'default', array('class' => 'alert alert-error'));
        }
    }
}
Andrew
  • 2,013
  • 1
  • 23
  • 38
  • What does your table definition look like? Have you tried to clear the cakephp model cache (app/tmp/cache/Models)? Also, set your debug level to 2 and check what SQL queries are executed to check if the correct queries are created. – thaJeztah Apr 15 '13 at 22:49
  • Hm, sorry, just realised that I misread your question, do I understand correctly that the data is already truncated inside `$this->request->data`? Have you tried to debug/trace the raw `$_POST` data? – thaJeztah Apr 15 '13 at 22:55
  • @thaJeztah that's correct that the data is already truncated inside $this->request->data. I have also checked the raw $_POST data and it's truncated there. When you say clear the model cache, should I just delete the Models directory? – Andrew Apr 16 '13 at 04:13
  • No, clear the *contents* of that directory, but as you mentioned that the data inside the `$_POST` is already truncated, I don't think that will be your problem. I was suspecting CakePHP to truncate the data when *saving* but apparently it's already in an earlier stage. If the `$_POST` data is truncated (which is very strange), it's best to narrow down the problem; try debugging `$_POST` at the *start* of `app/webroot/index.php`. If it's truncated there as well, then it has nothing to do with CakePHP, but PHP in general. Maybe a 'max_post_size` setting? A weird character in your data? – thaJeztah Apr 16 '13 at 06:53
  • I just found this question [PHP $_POST array variables are truncated](http://stackoverflow.com/questions/12684449/php-post-array-variables-are-truncated) related to a possible bug in PHP and [this article](http://arstechnica.com/civis/viewtopic.php?f=20&t=137482) points to a setting in the Suhosin patch. – thaJeztah Apr 16 '13 at 06:56
  • I checked the info file and the post_max_size is 8M. When I looked at firebug I noticed there there is a 302 Found status, and that the size is under 8M so I don't think it's an issue with Suhosin. – Andrew Apr 16 '13 at 13:18
  • @thaJeztah if you want to post an answer as it being a bug with PHP I'll accept the answer. There are several bug reports about this. I am testing on my localhost now, but the server that this will be moved to has a newer version of PHP, which will hopefully resolve the issue. – Andrew Apr 16 '13 at 14:05
  • LOL, shortest answer ever? Anyway, I hope your problem will be solved after moving to the new server. Good luck! – thaJeztah Apr 16 '13 at 14:38

1 Answers1

1

This problem may be caused by a bug in some versions of PHP that truncates $_POST values.

Without re-posting the information, this question contains some additional information.

PHP $_POST array variables are truncated

Some (possibly) related bugs;

Bug #42824 Post Data truncated

Community
  • 1
  • 1
thaJeztah
  • 27,738
  • 9
  • 73
  • 92
  • Lol I just found out what I was doing wrong. A varchar(500) is not 500 words, it's 500 characters. I still don't know why it was being truncated in Eclipse, but everything is being saved correctly now. – Andrew Apr 16 '13 at 18:22
  • erm, that was my first guess, but you mentioned *"...this->request->data and it has about 150 characters"*, implying it was already truncated ? – thaJeztah Apr 16 '13 at 18:42
  • Yeah, I don't know why it was like that. That threw me off too. When I set my debug point I inspected $_POST, $_REQUEST, $Globals, and $this in eclipse and all four of those places had truncated data in it. It would then truncate more after it saved to the db. – Andrew Apr 16 '13 at 19:07