Cake PHP useful tips View RSS

Interesting code, feature, hints, tips for programming in cakephp style
Hide details



I come back 22 Mar 2011 10:47 AM (14 years ago)

Hi everybody!

I have come back. I look - you read this blog and still have interest to CakePhp. So, I will write more and repost more interest and useful thing ;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Behavior in belongsTo request... 22 May 2009 4:26 AM (15 years ago)

As usual I was not looking for easy ways, and as usual faces challenges not addressed in book.cakephp.org ;-)

Always, the most troublesome part of the project for me is paginate and all that associated with it (sampling, sorting).

And now, I once again stepped on a rake, and the answer to my question - no.

So, what was the challenge:

Model A - hasMany Model B
Model B - belongsTo Model A

Model B - actAs ( 'Image')

Image - a wonderful Yevgenia Tomenko behavior, which is used to link entries in the model with the stored image.

In the case of a request Model B -> ... of any form - behavior of hours worked, and I got me the correct information - in this case, pictures.

However, when the problem began to build a query type

Model B -> Model -> A (in terms of where the sample is on the fields of Model A)

turned out - that the search and pedzhinatsiya work perfectly, but the results of Model B - no trace of the behavior ... I just got a field model, without the right to me «curvature».

No dancing, dancing with Contain, the order request, etc. - No effect. Well the answer is no, and guglegruppe.

And today I found another rake, but with behavior Translate. If you use the instructions and do as they advised - and it works fine, except my favorite pedzhinatsii ...

And it is - sort of fields located in the i18n table - you can not. Not possible. Only those fields that are present in an explicit form. Whew.

Of course, I do not want to say that sorting through the i18n table built to work quickly, or, in principle, be attributable to work. Maybe not. But in this case, if you need some sort of results except for id, created - no chance to use the behavior you do not. It does not work. : (

And if the rake can bypass the second request (first received the list of id Model B, and then to obtain directly from the Model B, all records (this is work behavior). In the second rake, the only way to sort - to dispense with boxed behavior.

But I have the trick ;-)

$results = this->Immovable->Best->find('all',array('conditions' => array('Best.country_id' => $country), 'limit' => 5, 'contain' => array('Immovable' => array('order' => array('Immovable.created' => 'ASC')))));

$result = $this->Immovable->Behaviors->Image->afterFind($this->Immovable, $results, false);

In this case, the results will be applied behavior

Cheers!

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

double HABTM how to? 8 Jan 2009 2:56 AM (16 years ago)

We have:

Post HABTM Tag
Post HABTM Category

We need:

Get all posts where Category.id = M, Tag.id = N
In CakePHP style, of couse ;-)

Scaffolding (just use cake console, nothing strange, all - typical).

Table:

posts: id, title, body
tags: id, title
category: id,title

join tables:

posts_tags: id, post_id, tag_id
categories_tags: id, post_id, category_id

How to?

Easy...

$this->Post->bindModel(array('hasOne' => array('PostsTag','CategoriesPost')));

(add param false if want to make paginate ;-)

And now:

$posts = $this->Post->find('all',array(
'fields' => 'Post.*',
'conditions' => array('PostsTag.tag_id' => 1,'CategoriesPost.category_id' => 4),
));

That's all, folks.

You can add params recursive = 0 (I think it good idea to reduce data in result)
You can use any cake-style 'or', 'and' in conditions query.

And this works well ;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Comments 5 Jan 2009 3:45 AM (16 years ago)

Dear readers!

Blogspot have too non-usability UI for comment reading and answering.

Yes. I read all your comments, but I remember - I saw some questions in your comments, but after "aprove" I cann't found comments ;-(

If you have question - ask me in twitter: cakephp
http://twitter.com/cakephp

I'll try answer to all your question with my bad english ;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

CakePHP 1.2 Stable is finally released! 26 Dec 2008 6:25 AM (16 years ago)

And now we have release ;-)

Merry Christmas and happy New Year and happy CakePHP! ;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

ACO\ARO - no problem! 5 Dec 2008 10:11 AM (16 years ago)

Recently, I finished my first application using Auth (ACO \ ARO). It turned out it was not scared.

It's simple, easy.

However, there are nuances. First hand accounts add all of these controllers ACO / ARO. Secondly Auth after login does not want to understand the prefix admin, and I get to the admin / posts / main - no way out, only to / posts / main. I had to write an additional line in routes.php

In general - I am happy. Very nice work. Faster and more convenient than dispose of around $ this-> obAuth-> check (...)

;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

CakePHP vs RoR 27 Nov 2008 12:42 PM (16 years ago)

Why you still with CakePHP? Why don't switch onto Ruby on Rails?

Just interesting to know...

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

ACL ACO Howto... 23 Nov 2008 11:09 AM (16 years ago)

Just do it few simple steps, and you haven't any more questions about ACL|ARO...

http://book.cakephp.org/view/641/Simple-Acl-controlled-Application

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

CakeBook source 20 Nov 2008 12:22 PM (16 years ago)

And now You can read and study on CakeBook (book.cakeforge.org) sources code ;-)

http://thechaw.com/cakebook

I'm going for popcorn ;-)

You see? Guys work on CakeBook over 39 years! Starting in 1969... Nice hippy time.. Love drugs and rock'n'roll ;-)

So it's funny, but really - very nice time to start learning how to write code RIGHT

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

PHP 2008 18 Nov 2008 2:06 AM (16 years ago)

Asked to look and move the site, written by one system administrator. The site is written in php without a bare framework.

What do you say friends? At the yard - in 2008, and within - solid global variables.

And type:

INSERT INTO ... ". $ _GET [" Param1 "] - and does, at every turn.

As their site is not hacked so far - do not understand

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

ACL ACO ARO 2 Nov 2008 6:00 AM (16 years ago)

Don't understand....

Terrible things

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Automobile project 27 Oct 2008 11:20 AM (16 years ago)

Hi, friends!

I was silent a long time, because it was a lot of work. Yes, despite the looming default in our country (I hope I am wrong). Like most of my friends, we work, work, work ...

Now I want you to submit a site devoted to selling cars - prodam-auto.com. The site in Russian, so forgive me, the English-speaking friends (google will help you to understand that where and how).

The site just completed its local run and fixing errors. Now going on alpha testing.

I have already found a couple of points that I missed in the development of the site. Now I think whether it is a mistake, or feature ;-)

Thanks to that tolerate me!

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Twitter 8 Oct 2008 11:33 PM (16 years ago)

I think the twitter is more useful than short message in blogger.com

So, welcome to my twit: www.twitter.com/cakephp ;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Browsers 10 Sep 2008 12:27 AM (16 years ago)

I went to see w3school statistics browsers… However, employment trend emerges.

As this is still statistics, that is close to the "Rules on Water" does not look at absolute figures, but on the relative changes…

2004, IE = 76.2%

2005, IE = 68.9% Fx 23.6%

In 2006 IE = 60.6% Fx 29.9%

In 2007 IE = 57% Fx 36.3%

2008, IE = 50.6% Fx 43.7%

Even if there plus minus 5-10% in accuracy. The trend, nevertheless, to the person. And yet here, I think Google Chrome will make serious amendments to the statistics.

According to statistics LiveInternet for September 1-7 at GC 0.6%, for 8-14 September (but today only 9 - e!) - 0.8%

Less percent, is, of course drop in the ocean. But this is not two weeks have passed since the advent of the browser. All the more useful feature such as - creating web application - again, this is NOT FireFox from "unclear whom", and the great Google (! That for companies and offices important), fast (as promised) processing Javascript that in my opinion it is not important, but arhivazhno, Google Chrome opportunity to establish normal user under Windows without resorting to the services administrator (not personally experimented, but overhear in podcasts).

IE as I see it, CMS for the site can be targeted at Google Chrome (almost Safari), a company with more likely to agree to set Chrome (FF on - the refusal of nearly 100% - I do not know why). And fast work with JS - also can not fail to please users jquery / prototype and (especially) extjs.

Honestly admit, I personally Google Chrome has not yet watched. Wait a formal assembly under poppy (Google shame!)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Typical dialogue with the client 5 Sep 2008 3:03 AM (16 years ago)

Customer: Hello. I am here for you e-mail sent, with a list of sites which I like. Look.

Me: (I looked). Why do you sent me html? Why could not link?

Customer: What is the difference?

Me: There are. Okay. (Open html-file search for a direct link to the site. Open site)

Customer: I decided that I did not write it like that do not like. I better explain in words. So will be faster.

Me: Do you think that I remember it all?

Customer: Are you inscribe.

Me: 8-O (curtain)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Pipes.Yahoo 503 error 4 Sep 2008 7:00 AM (16 years ago)

Recently I met to do a careful study yahoo pipes, as it turned out - something is broken, and pipes.yahoo do not work. Everywhere error 503.

Even previously saved to open pipes is not possible. Judging by reports on the forum - a problem with the 7-8 morning.

And I just encountered a problem that String.Translate module converts any field except the field content: encoded. And I just met to find out what the problem is - 503 :(

Well then. Let's wait. Meanwhile, loans something more useful. For example CakePHP :)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

dateTime bugs 3 Sep 2008 9:20 AM (16 years ago)

Strange error in the helpers who is responsible for form:

echo $ form-> dateTime ( "Showtime", "DMY", 24, null, array ( "minYear" => 2008, "maxYear" => 2012, 'monthNames' => false, "interval" => 15) false);

All right? Everything!

However, in html see the following code:

< name="data[Showtime][Showtime][day]" id="ShowtimeShowtimeDay">
< value="01"> 1
...
< / select> -
< name="data[Showtime][month]" id="ShowtimeMonth">
< value="01"> 01

Why do we have chosen day encoded twice Showtime? Other parameters are coded properly. If only not forget that in order to set the interval mode 24 hours a day, should be a bit sub-source code (ticket already established).

We are talking about CakePHP 1.2 RC2

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Multilingual project, i18n, poEdit - in CakePHP 3 Sep 2008 12:38 AM (16 years ago)

PoEdit - excellent piece for the "transfer" in the draft texts. Installed - editor works, but there are features.

First, poEdit unwanted collect data from catalogues CakePHP - reported that nothing was found.

Secondly, while maintaining po / mo files - reported error (but not scared, the files still persist).

The first had to deal means CakePHP:

bash cake i18n - ask the location project, and the cake itself forms the pot file for poEdit.

We are talking about poEdit for MacOS.

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

CakePHP 1.1 is dead now 30 Jul 2008 1:40 AM (16 years ago)

If you are new user of CakePHP, forget about CakePHP 1.1 (stable). Learn CakePHP 1.2. 


Yes, it's just RC2, but very useful, nice working and stable ;-)

CakePHP going to past... So long, pal...

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Zyxel, MacBook & WiFi troubles 17 Jul 2008 5:46 AM (16 years ago)

As you know, I'm a happy owner a Apple Macbook. Also, for home mobile development ;-) I get Zyxel 660HTW2EE (ADSL modem + router + WiFi).


But my happy wasn't long ;-(

MacBook drop WiFi connection every 30-60 minutes (when WPA2-PSK). Or drop connect and Zyxel make could restart (!). And I have 3-5 minutes without Internet, without network (terrible! ;-)

When I'm switching into WPA-PSK mode, I get stable connection, Zyxel hasn't restarting. But speed is so slow ;-( max - 110KB/sec (It's 802.11g mode!) Terrible..

After reading a lot of forum/blog and manuals I switch off any WPA, and setup MAC-Filter for my MacBook. Now I have stable connect. STable working of Zyxel, stable Wifi in MacBook.

You may ask me - why you don't by AirPort from Apple? 

I have answer ;-) 
Because AirPort doesn't have ADSL modem ;-( Yep. In my region we using ADSL internet. still... 

So, If you happy from your Zyxel & MacBook - switch off any WPA/WPA2 coding, setup Mac Filter and have fun with WiFi Internet on  your notebook...

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Great thing : bindModel 14 Jul 2008 5:24 AM (16 years ago)

At present moment a have a lot of free time for self improvement and I can learn CakePHP deeper and deeper, as it possible ;-)


In one of my project I have two table with simple relation

Division (id, title) - hasMany Staff
Staff (id, division_id, name, jobtitle..)

So. Now I need to get some info like: I need to get all Staff where birthday today, and get result like:
Array (
   [0] => ["Division"] = Array ...
         => ["Staff"] = [0]
  [1] ...

But I don't want (or cann't) rewrite model fore staff/division. So, What I must doing?

Of cause I must to use this wonderful thing - bindModel ;-)

Look

$this->Division->bindModel(array("hasMany" => array ("Staff" => array (
"conditions" => array ("MONTH (birthday) = " . $month, "DAY (birthday) = " . $day),
"fields" => array ("Staff.id,Staff.name"),
"order" => array("Staff.name" => "ASC")))));
$staff = $this->Division->find("all");

Also, we can put in field "fields" something like this
"fields" => array ("COUNT (Staff.id) as count")

And we have array with Division and numbers (count of staff with correct birthday)

Simple? yep. And in good cakephp tradition ;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

MAMP & cake (terminal) 10 Jul 2008 11:50 AM (16 years ago)

YESS! I did it! ;-)


1. sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
2. And 1 hour looking for bug, and found mistake in database.php

After some experiments I put host => 127.0.0.1
And forget about it ;-)
Now all cool -> host -> localhost
And my cake bash baker work fine

I am happy! I am like unixman ;-))))

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Hm. Cake 1.2RC2 & baker 9 Jul 2008 11:20 PM (16 years ago)

It's very strange, but I cann't use this cake baker under MacOS ;-(


If I starting terminal cake (from pack dmg) I can read only help and resume: Process completed. And could not put any symbol

If I starting terminal, and go to my site directory and run:

bash cake bake

I can create "skeleton" of my web application in directory myapp. Now cake not want to use this subdir and I'm moving files into /cake/console/..

After this I type: bash cake bake
& setup database.php setup. Of couse, I know - this file has not mistakes (Yep, I'm copy database.php into site/app/config/ and read this "Cake is able to connect to database". So - My database setup is nice, and MySQL server work properly.

So. I returning into cake/console, and run bash cake bake - for creating model. But, after I choose database config "default" (yep My database config is really calling default) I get 3 warning about impossible to connect to database throw sockets and get finally message about "database haven't any tables" but this is wrong! I have tables.

And of cause, I'm using MAMP

Where is mistake? I don't know ;-(

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Cake...dmg & .htaccess 8 Jul 2008 9:39 PM (16 years ago)

After my switching into MacOS I'm start to use cakephp.dmg with latest version of this wonderful framework. But I found some troubles with use this image (dmg).


When MacOS mount cake.dmg you can drag & copy content of framework into your project. Easy? Of cause! But you cann't drag .htaccess from the root of the cakephp. 

May be it's a stupid problem, and after setup finder folder view you can move even invisible files, but I don't know how to made this. After all I'm install muCommander and copying this .htaccess file from mu. How to make this copy more easy?

Yes, .htaccess from /app/ & /app/webroot - copying to project without any problem.

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?

Unix hosting 8 Jul 2008 2:30 AM (16 years ago)

My pal (pal by ICQ ;-) broken mind about simple problem...


Pre...

On localhost all work nice. Stable and fine. (Windows)

After uploading to unix hosting my pal get some troubles with error "Model not found", "Controller not found" etc...

But, of couse, file posts_controller.php, post.php are present in bot copies of site.

Decision: unix-style hosting known file Post.php and file post.php - it's really different file. Not same. 

So, if both copies of your site identical - don't forget to check filename uppercase & lowcase ;-)

Add post to Blinklist Add post to Blogmarks Add post to del.icio.us Digg this! Add post to My Web 2.0 Add post to Newsvine Add post to Reddit Add post to Simpy Who's linking to this post?