CakePHP: The Rapid Development Framework for PHP - Official Repository CakePHP v3.0 - the rapid development php framework.

Using CakePHP FormHelper with Bootstrap Forms

CakePHP's FormHelper is how you generate forms when making CakePHP applications. As one might assume, this includes generating input elements, like so:


Which will produce HTML something like this:

<div class="input text">
  <label for="ModelAbc">Abc</label>
  <input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">

Now, sadly, Bootstrap wants something like the following:

<div class="control-group">
  <label for="ModelAbc" class="control-label">Abc</label>
  <div class="controls">
    <input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">

How do I make CakePHP produce this output?

Source: (StackOverflow)

save() returning false, but with no error in CakePHP

My debug value is set to 2, and it's displaying all the queries, except the one I need.

I have an Items controller method that is calling this method in the User model (Item belongsTo User):

function add_basic($email, $password) {

        'email' => $email,
        'password' => $password

    if($this->save()) {
        return $this->id;
    else {
        return false;

I have confirmed that $email and $password are being passed into the function correctly (and are populated with legit data). email and password are the names of the fields in the User model.

I have also confirmed that on $this->save() it is returning false, but when I view the page where this occurs, the query is not being printed in the debug, and there is no error being thrown, so I have no idea whats going wrong.

Any ideas on how I can see the error, or why the query doesn't seem to be getting executed?

It's weird, cause right after this, I have another model saving data to it in the exact same fashion, it goes off without a hitch.

Source: (StackOverflow)

Asynchronous processing or message queues in PHP (CakePHP)

I am building a website in CakePHP that processes files uploaded though an XML-RPC API and though a web frontend. Files need to be scanned by ClamAV, thumbnails need to be generated, et cetera. All resource intensive work that takes some time for which the user should not have to wait. So, I am looking into asynchronous processing with PHP in general and CakePHP in particular.

I came across the MultiTask plugin for CakePHP that looks promising. I also came across various message queue implementations such as dropr and beanstalkd. Of course, I will also need some kind of background process, probably implemented using a Cake Shell of some kind. I saw MultiTask using PHP_Fork to implement a multithreaded PHP daemon.

I need some advice on how to fit all these pieces together in the best way.

  • Is it a good idea to have a long-running daemon written in PHP? What should I watch out for?
  • What are the advantage of external message queue implementations? The MultiTask plugin does not use an external message queue. It rolls it's own using a MySQL table to store tasks.
  • What message queue should I use? dropr? beanstalkd? Something else?
  • How should I implement the backend processor? Is a forking PHP daemon a good idea or just asking for trouble?

My current plan is either to use the MultiTask plugin or to edit it to use beanstald instead of it's own MySQL table implementation. Jobs in the queue can simply consist of a task name and an array of parameters. The PHP daemon would watch for incoming jobs and pass them out to one of it's child threads. The would simply execute the CakePHP Task with the given parameters.

Any opinion, advice, comments, gotchas or flames on this?

Source: (StackOverflow)

CakePHP find method with JOIN


I need to do the following query using the CakePHP find method:

FROM `messages`
INNER JOIN users ON messages.from =
ORDER BY messages.datetime DESC

Basically I have:

  • messages table with a Message model
  • users table with User model

and want to retrieve information from both tables in one query. The field is the same as the messages.from field, so that's what the join is on.

I am doing it in my MessagesController so it would need to be something like:



Source: (StackOverflow)

What new features and improvements does Lithium provide over CakePHP?

I've used CakePHP on several projects in the past, and have more recently started using Ruby on Rails, but there's a new project I'm about to start that will require PHP. While refreshing myself on CakePHP I learned that there is a new framework called Lithium that is essentially what CakePHP 3 was going to be. It's being developed by a group of former core CakePHP devs.

I haven't found a whole lot of information about it since it's still under development status, but I was wondering if anyone knows (or has a link to) some information on what benefits it provides over CakePHP. Hopefully something a bit beyond the quick overview shown on the official site. I'm trying to decide whether to use CakePHP for my upcoming PHP project or to wait a bit for Lithium to release a non-development version and try that out.

Source: (StackOverflow)

Speeding up CakePHP

I've been a keen fan and user of CakePHP for about 2.5 years now, but the main bugbear that most fellow developers level at the framework is that it's slow, and the dispatch cycle takes too long to make it a viable solution for production environments.

I'm hoping that this question will inspire people to share their tips, tricks and hacks for speeding up CakePHP performance.

The blog post I most often refer to is here, Which has great tips, but there must be more out there!

So please feel free to share your thoughts on making this excellent framework that much more nimble!

Source: (StackOverflow)

The processing instruction target matching "[xX][mM][lL]" is not allowed

I'm outputting XML in CakePHP. But I'm getting this error when I put my XML into a validator:

The processing instruction target matching "[xX][mM][lL]" is not allowed.

I am using correct XML layout with <?php echo $this->Xml->header(); ?> at the top then <?php echo $content_for_layout; ?>

My results are:

<?xml version="1.0" encoding="UTF-8" ?><response type='' outcome='true' message='Login successful!'>
    <user id='1234' total_number_of_completed_tasks='0' total_number_of_declined_tasks='0' total_number_of_passed_tasks='1' total_number_of_failed_tasks='1' reputation_points='99' deviant_points='0' />
                                <accepted_task id='4' type='Good' time_limit='500' details='Good accepted' />
                                <accepted_task id='5' type='OK' time_limit='660' details='Ok New task' />
                                <accepted_task id='9' type='Excellent' time_limit='2000' details='Great failed task' />
                                <accepted_task id='11' type='Your type' time_limit='222' details='Running and swimming all the way to Japan' />
                                <accepted_task id='7' type='Man' time_limit='744' details='My dirty task' />
                                <pending_task id='8' type='Women' time_limit='5151' details='Women new task' sender_id='11111' sent_date='2031-01-01 00:00:00' sender_name='Jae Choi' />
                                <new_task id='5' type='OK' time_limit='660' details='Ok New task' />
                                <new_task id='8' type='Women' time_limit='5151' details='Women new task' />
                                <new_task id='4' type='Good' time_limit='500' details='Good accepted' />
                                <new_task id='10' type='Hello' time_limit='122' details='What is this?' />
                                <new_task id='3' type='Best' time_limit='880' details='Stop doing work!' />
                                <new_task id='11' type='Your type' time_limit='222' details='Running and swimming all the way to Japan' />
                                <new_task id='6' type='Great' time_limit='553' details='Great accepted task' />
                                <new_task id='7' type='Man' time_limit='744' details='My dirty task' />
                                <new_task id='9' type='Excellent' time_limit='2000' details='Great failed task' />

Is there anything wrong with this?

Source: (StackOverflow)

How to unit test your API?

I'm at the point where I need to write unit tests for a REST API written using CakePHP 1.3. The API supports GET, POST and PUT requests for querying and manipulating data.

Is there any established way to test the correct input/output of an API simulating an HTTP request, using fixtures? I do not want to run actual POST/PUT requests against the live (dev) database. How can I best mock out the system to use temporary models, yet test the rest of the stack as-is?

Testing GET requests is easy enough with controller tests. However, for data manipulation the API uses HTTP headers quite extensively and also parses raw XML and JSON POST/PUT data. The controller unit test methods only mock POST data by setting $this->data in the controller, which does not allow me to properly test the API.

Source: (StackOverflow)

Set Response Status Code

I have an API call for which I need to be able to run some checks and potentially return various status codes. I don't need custom views or anything, I just need to return the proper code. If the user hasn't passed proper credentials, I need to return a 401 status. If they haven't sent a supported request format, I need to return a 400 status.

Because it's an API, all I really want to do is set the response status and exit with a simple, stupid message about why the request failed (probably using a exit). Just enough to get the job done, but I haven't been able to get this to work right. I've tried using PHP's header() and Cake's $this->header() (this is all in the controller), but although I get the exit message, the header shows a 200 OK status.

Using the code below, I get the message, but the header isn't set. What am I missing?

  if( !$this->auth_api() ) {
    header( '401 Not Authorized' );
    exit( 'Not authorized' );

Source: (StackOverflow)

Unit testing in CakePHP? [closed]

I'm wondering, how do you guys unit-test in CakePHP? How do you incorporate tests in projects? What parts of a project do you test? How do you decide which parts gets to be unit-tested? Do you guys still get to finish the job before the deadline?

Source: (StackOverflow)

CakePHP: Call to a member function setFlash() on a non-object

I get the following error when trying to logout of my CakePHP app:

Notice (8): Undefined property: UsersController::$Session [APP/controllers/users_controller.php, line 75]
Fatal error: Call to a member function setFlash() on a non-object in /Users/cameron/Sites/cakeapp/app/controllers/users_controller.php on line 75

This is the code for lines 74, 75 and 76:

function logout() {

Source: (StackOverflow)

Fat models, skinny controllers and the MVC design pattern

I just read a blog post that explains MVC with a banking analogy. I have a few months of experience with web application development with an MVC framework (CakePHP), so I get the basics, but I began to see a theme that made me think I'm taking a flawed approach to where I put my logic:

  • Fat models, skinny controllers
  • Keep as much business logic in the models as possible

In my app, models are anorexic and controllers are obese. I have all business logic in the controllers and nothing besides associations and validation rules in the models.

Scanning through my controllers, I can now identify a lot of logic that should probably go in a model:

  • The app has lists, which contain items, and the items can be ranked. The sorting logic which puts the list in ranked order is in a controller.
  • Similarly, items (Item model) also have images (Image model). Each item may have a default image (designated by image_id in the items table). When an item is displayed with its images, the default image should appear first. I have the logic that does this in a controller.
  • When a list is displayed, related lists are displayed in the sidebar. The logic to determine which lists are related is in a controller.

Now to my questions:

  1. With the examples I gave above, am I on the right track in thinking that those are instances of logic presently in a controller that belongs in a model?
  2. What are some other areas of logic, common to web apps, that should go into models?
  3. I'm sure identifying this problem and changing my design pattern is half the battle, but even if I decide to take those examples I gave above and try to move that logic to a model, I wouldn't know where to begin. Can anyone point me in the right direction by posting some code here, or linking to some good learning resources? CakePHP specific help would be great, but I'm sure anything MVC will suffice.

Source: (StackOverflow)

symfony vs cakephp [closed]

What is conceptually the difference between symfony and cakephp?

Source: (StackOverflow)

Can I use one model inside of a different model in CakePHP?

Can I use another Model inside one model?


class Form extends AppModel
    var $name='Form';
    var $helpers=array('Html','Ajax','Javascript','Form');
    var $components = array( 'RequestHandler','Email');

    function saveFormName($data)

    function saveFieldname($data)


Source: (StackOverflow)

How can I see CakePHP's SQL dump in the controller?

Is there a way that one can cause CakePHP to dump its SQL log on demand? I'd like to execute code up until a point in my controller and see what SQL has been run.

Source: (StackOverflow)