1 . Coffeescript and CakePHP?

Answer :

I usually handle this with a build script, either in Bash or Rake (yes, Rake, its great for building PHP projects too), which compiles, concats and compresses my .coffee files for production. While in development I use coffee -w to compile the .coffee files on the fly.

You could automate this further by adding a post-commit hook to Git or Subversion if you like.

You should be able to extend Mark Story's Asset Compress plugin to do the compiling on the fly as well.


Leave a Comment

Name  
  Email   
Message
1 . Escape from CakePHP Paginate condition?

Answer :
$this->paginate = array(
    'conditions' => array(
        'Product.original_price <= 77'
    ),
    'recursive' => 2,
    'paramType' => 'querystring',
    'limit' => '25',
    'maxLimit' => 100,
);
$records = $this->paginate('Product');

Instead of key => value just use value. But usually it doesn't matter if number is enclosed with quotes, even with varchar.


2 . Installing Wordpress along side Cakephp?
 

Answer :

One thing that is asked quite a lot on #cakephp is how to use other apps alongside CakePHP, and the answer giving is normally pretty ugly. Stick the files/folders in side webroot/. Although that does work, its not very nice. So ill show you a little trick with .htaccess files.

The first (really simple way) is to use a .htaccess inside the sub folder. For example you can have a copy of Joomla! running alongside cake with no issues when you have the .htaccess for Joomla! enabled. If the app does not have one and/or you would not know what to put in the .htaccess file you have another option

Make Apache stop processing rewrites if it finds a request for your sub directory. This is done by adding a rule for the sub directory and then telling Apache it is the last rule to process. The .htaccess file you want to edit is the one found inside your APP directory. All you want to add is the following line:


3 . CakePHP Plugin CSS?
 

Answer :

it should be

echo $this->Html->css('/install/css/default');

also note the lowercase i


4 . CakePHP shell unit tests?

Answer :

Judging from examples in Mark Story's AssetCompress and the CakeDC's Migrations, just mimic the directory structure for other tests:

Test/
  Case/
    Console/
      Command/
        Task/
          MyShellTaskTest.php
        MyShellTest.php

Your tests can just extend the CakeTestCase object, just like any other generic test:

class MyShellTest extends CakeTestCase {
}

If needed, you can override the base shell, just like you would do with a Controller test:

class TestMyShell extends MyShell {
}

Nothing all that special, just stick with the conventions.


5 . Download cakephp manual?

Answer :

Downloading a manual is strongly discouraged because it is alive in the same way that development is and subject to improvement and change. I recognise,however, the need for an offline copy and you can find instructions on where to look and how to build your own 

It's pretty straightforward. The cake links point to all-in-one-page versions which are useful in their own right for faster searching using ctrl-f



6 . Cakephp and Yii: similarities/differences/speed?
 

Answer :

From personal experience, there's several difference in both frameworks, particularly the way models are handled in cake vs yii.

  • In cakephp you cannot use composite primary key, this is the main deal-breaker for me. Composite primary key is supported in Yii.
  • Cakephp enforces convention, while yii favors convention, you are not forced to follow a certain standard. While enforced convention is a good way to build good programming practice, in the end the flexibility that yii offered wins this round for me.
  • Out of the box performance, I think yii has the advantage, but as with performance tuning, with proper cache configuration, and as long as you are careful in using the ActiveRecord patterns, they are not that much apart

As a side note, I have lead web app implementation with both CakePHP and Yii, the feedback that I got from the team was that the less experienced team preferes CakePHP, while Yii is preferred by the more experienced team. Make of that what you will.


7 . CakePHP/CakePHP 2.4 app?
 

Answer :

Of course, use the schema dump command from the Cake Console.

It will write the entire schema to a .sql file and store it in App/Config/Schema.

Example of usage:

Console/cake schema dump --write filename.sql

(change 'filename.sql' to whatever the dump file should be called.)

This can also be find in the cake docs: 


8 . Facing a file permission error while running CakePHP in Ubuntu 10.4?

Answer :

The command sudo chmod -R 777 cakephp/app/tmp only made tmp writable, you should make cache and it's subdirectories writable as well, otherwise Cake can't write the cache files to the cache directory in tmp.

So, these directories should be writable:

cakephp/app/tmp/cache
cakephp/app/tmp/cache/persistent
cakephp/app/tmp/cache/models

Make sure the log directory is writable as well: cakephp/app/tmp/logs


9 . Installing cakephp 3 manually, without composer?

Answer :

Packaged app (cakephp/app) releases that include all dependencies (framework (cakephp/cakephp), standard CakePHP plugins (cakephp/debugkitcakephp/bake, etc), required third party libraries) can be found on GitHub.

It's the download with the small package symbol, named like cake-3-x-x.zip.

However, it isn't a good idea to ditch the dependency manager, as keeping the code base and the autoloader up to date will be rather tedious, and, no offense, I have my doubts that you'll be able to handle this properly if you don't even know how to stitch the app and cake packages together.


10 . Speeding up CakePHP?

Answer :

I think this is a really good question. Here are a couple things I do to speed up cake apps.

  1. As mentioned in the comments of the linked article, cutting down on the $uses array helps a little. You can access associated models by going through their associationg. So if City and Address where associated, you could access address by $this->City->Address instead of including both in the $uses array

  2. In apache, move the code from your .htaccess into the main server config/vhost/whatever and set AllowOverride None.

  3. In a load balanced environment, move sessions from the DB to memcache. Memcache is easy as hell to setup, and the cake's DB session class leaves much to be desired. In high load application the garbage collection will kill you, as it ends up running every second or so. Also, here's a great little script that gives you stats about your memcache usage (based of apc.php)

  4. As Mark Story mentions in the comments section of the 8 ways article, compressing your assets is a very good idea. Here is a good script that minifies both js and css files, which can be used to replace the default css.php that comes bundled with cake.

  5. If for some reason people hit alot of images/css/js that don't exist anymore, it might be beneficial to make sure those pages do not generate a cake 404 error, as it has to go through the whole cake dispatching process, generates a session etc. It's as simple as changing this:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

    to this:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/(img|css|js)/(.*)$
    RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]


11 . CakePHP with Stored Procedure?

Answer :

Breaking down the snippet

$this->query("CALL `$name`($parameter);");

calls stored procedure. Lets assume that the procedure's body is a simple SELECT

SELECT table.* FROM table;

This procedure returns a result set. Take a quick look at the code you provided

 $this->query("CALL `$name`($parameter);");

Yes, the procedure is called but the result/resource is not assigned to a variable to iterate over.

$proc_result = $this->query("CALL `$name`($parameter);");

$proc_data = array();

if (false !== $proc_result)
{
    while ($proc_row = mysqli_fetch_array($proc_result))
    {
        $proc_data[] = $proc_row; 
    }
}

if (!empty($proc_data))
{
    //do whatever with procedure data
}

12 . CakePHP 3 - Where is custom datasource?

Answer :

Just look at the code of any existing database driver and see how it is built? There is nothing in the book yet about how to create your own datasource.

ArangoDB seems to be yet another NoSQL DB, so take a look at how this Elastic Search datasource is done. By a quick look I think you can use it as a base for your implemention, they seem to be similar.


13 . Cakephp 1.3 url rewriting?

Answer :

The conditional check isn't in the code in /cake/libs/views/pages/home.ctp - this was a bug or oversight in the recent release.

If you copy that file to your /app/views/pages/home.ctp you can easily enough edit the block that makes that check out of the default homepage. Or better yet, you can simply create your own custom home.ctp.


14 . CakePHP business logic layer?

Answer :

No. It sounds to me that what you are running into is the classic downside of the Active Record pattern. Also, it doesn't help that CakePHP is all based around result associative arrays instead of object instances. I suggest that you take a look at packages like Doctrine 2. It implements a DataMapper pattern instead of an ActiveRecord pattern. It keeps your business logic completely separate from your data access layer.

There are CakePHP extensions to integrate Doctrine into CakePHP.


15 . Angular - CakePHP integration?
 

Answer :

Actually it is possible to use angular on apps that are not following the single page approach. You can use directives and controllers directly inside the HTML of every page.

Authentication is not an issue in this case as the server knows who you are based on the session, it should be present. This is actually what we do in a legacy app that might become a SPA in the far future.

If you need authentication between requests I would recommend to use JWT tokens. There is even a plugin for CakePHP that implements a proper auth adapter for JWT:  I've even written an article about that topic: