cakephp print a boolean - cakephp

Beginners question in cakePHP, it drives me mad, I tried so much but can't get this working in cakePHP.
I want to print 'Train station nearby' if the boolean is set.
The database has the field 'train' bit(1) when doing a
<?php echo h($property['Property']['train']); ?>
it shows 1 but in the database it is 0, WHY is it printing 1 instead of 0
<?php if($property['Property']['train'] == true ) echo 'Train station nearby'; ?>
This output works all the time, but of course it is not true!
Anyone please, is this something to do with the fact it is a bit(1) field or am I doing something wrong. Please keep in mind I am a beginner, and I did try a lot of php examples with booleans, but just can't work out why it is not working for me in cakePHP.
Thanks for looking into this.

If you are using mysql use tinyint(1) for the field which is emulated as boolean.

According to this ticket bit type fields are currently not supported by CakePHP. Instead, use the boolean type for boolean values.

Related

Multiflash session in cakephp

I need to send more than 1 session flash with cakephp, I have found some solutions how to create a for loop and put together a deal but wanted to know if there was any native function of cake for something.
Two days ago I wrote http://www.dereuromark.de/2014/04/21/cakephp-flash-messages-2-0/
Which basically added support for this in CakePHP1.x, and therefore also for CakePHP2.x now.
It stacks multiple messages per type, as well. Details see the Wiki.
Its really not clear what you really wants- but to create multiple flash message as on documentation-
// set a bad message.
$this->Session->setFlash('Something bad.', 'default', array(), 'bad');
// set a good message.
$this->Session->setFlash('Something good.', 'default', array(), 'good');
And on your view-
echo $this->Session->flash('good');
echo $this->Session->flash('bad');
And there is a Helper you can checkout- MultiFlashHelper

CakePHP 1.3: Issue with saveField

I dont understand why I am not able to update a field on the database based on the following code:
$this->User->id = 1;
$this->User->saveField('image','img/default_pic.png');
Basically, I want to change the current image in the Db with a new one.
The code above just clears the value that is currently in the image field, but does not add anything.
As an example, this is what happens:
id username image
=============================
1 admin mypic.jpg
2 john johnPic.jpg
After the code above is executed I get the following result
id username image
=============================
1 admin
2 john johnPic.jpg
I am confused at what is actually happening
SOLVED!
I decided to go back and check on my user.php model class and realized that I had attempted to use MeioUpload before and gave up, but I never removed the var actsAs entry.
As soon as I commented it out, I am now able to upload pictures.
For anyone else that might come across this issue, beforeSave might also caused these kind of problems, according to the following blog: http://blog.phplabs.net/2011/11/cakephp-savefield-not-working.html
Thanks,
What does the sql log say is happening?
My guess is you are actually doing something like
$this->User->saveField('image', $variable)
and $variable is either misspelled or is empty.

cakephp data getting posted

I have a form and I want to see it in fiddler that what data is getting posted when form is submitted. I am not able to see anything. How do u debug in cakephp. I am a newbie in it
thanks
you must have Configure::write('debug',2); defined.
then you can use debug($data); to debug any data.
also in views or layouts you can have
<?php echo $this->element('sql_dump'); ?>
to output any database queries that had took place.
Inside app/config/core.php make sure the 'debug' value is 1 or 2 . t
If you use 1 warning and errors(if exists) will display
If you use 2 warning and errors(if exists) will display and all the sql query will display(that queries are runs on that page)
Within app/config/core.php make sure the 'debug' value is greater than 0.
For any value you would like to see, call the Cake 'pr' function to output it to the page. pr() will automatically expand any nested arrays and data structures.
you have to print data by using
echo "<pre>";
print_r($this->data);
echo "</pre>";
in this,Not required to 'debug' value is 1 or 2 .

Showing an input in a view depending on role

I have a fat view (add) with a lot of inputs (it's a form).
For a role , there is a user_id input that is necessary, but for another role it isn't necessary. Here it comes my question, I know that elements are used for this kind of things, but I would be duplicating a lot of code if I make a view with this input and another one without it. Is there any way to just replace a line of code depending on the role? How would it be?
Thank you very much in advance, have a nice day!
What's wrong with a good old if?
<?php if ($theRoleOfTheUser == 'someSpecificRole') : ?>
<div><?php echo $this->Form->input(…); ?></div>
<?php endif; ?>

CakePHP IBM Tutorial: Incorrect API doc for Model::validate()?

Okay, this is driving me nuts. I’m working through the IBM CakePHP Tutorial, and in the first part, I’m at the section where the author is introducing validation rules for form input:
www.ibm.com/developerworks/opensource/tutorials/os-php-cake1/section5.html#N107E3
For the life of me, I can’t figure out what’s happening in this line of code:
$this->invalidate('username_unique');
According to the CakePHP documentation, the Model::invalidate() method takes as its first parameter a string that specifiies “The name of the field to invalidate”. How is “username_unique” the name of the field to validate? Looks to me like it should be just plain old “username”. But incredibly enough, the author’s code works, and mine doesn’t when I change “username_unique” to “username” (or even “User.username”), so I’m thinking there might be a serious flaw in the documentation (or very possibly, with me).
[FWIW, I can see that the CakePHP 1.25 provides a better means of doing validation, but I still find it troubling that what seems to be a well-documented method doesn't seem to be doing what it advertises, and I want to understand why the tutorial code works.]
Can anyone shed any light on this?
The "magic" is actually in the $form in this case.
When calling $this->invalidate('username_unique'), Cake takes a note that the field username_unique is invalid. The fact that this field does not actually exist is irrelevant.
Now, take another look at the actual $form field (slightly reformatted):
echo $form->input('username', array(
'after' => $form->error('username_unique', 'The username is taken. Please try again.')
));
It's outputting a normal form field, but "manually" places an error() output after the form field. $form->error('username_unique', $message) means "if there's an error for the field username_unique, output the message $message". So you're actually marking an imaginary field as invalid and are manually outputting an error message for this imaginary field.
And actually, that's a load of outdated cr*p you should forget right away. There's a built-in syntax for multiple validation rules per field, so you can test for character length and uniqueness at the same time and even get different error messages for each error type. There's even a built-in isUnique rule, so you won't even have to code a manual uniqueness test.

Resources