Tag archives for CakePHP

  1. CakePHP 2.1.4, 2.2, and a pick into 3.0

    By Leonid Mamchenkov

    There’s been a stream of good news from the CakePHP headquarters recently.  If you are as slow as me on catching up with these things, here is a quick summary.

    • CakePHP 2.1.4 has been release, and that’ll be the last release for the 2.1 branch.  It’s time to move on.
    • CakePHP 2.2 stable has been released, and that’s what you should be using for your projects.
    • CakePHP 3.0 has been mentioned, so if you are interested in contributing early, here is your chance.

    CakePHP 3.0 will take a few month to develop.  Mainly, the work is focused around the following:

    • Drop support for PHP 5.2.
    • Add and improve support of PHP 5.4+.
    • Reorganized CakePHP classes to use namespaces to avoid collisions with other libraries and classes.
    • Improve bootstrapping for better control by developers.
    • Rewrite the model layer to support more drivers, object mapping, richer API, etc.
    • Rewrite the routing to work faster and be more flexible.

    Overall, it looks like some really healthy activity in CakePHP project.

  2. CakePHP 2.0 released!

    By Leonid Mamchenkov

    I’ve been a bit all over the place these last few days, but I knew that this was coming shortly – CakePHP team released the new and much improved version 2.0 a couple of days ago. There are a lot of changes. And I do mean a lot. Here are some of my favorite:

    • PHP5 support. CakePHP was working with PHP before, of course. But in this release, support for PHP4 was dropped and all code has been updated to utilize PHP5 goodies.
    • Lazy class loading. Previous versions of CakePHP could easily get slow with a lot of models. There were solutions like Containable behavior and others. But it was annoying never the less. CakePHP 2.0 is much improved in this regard. It only loads things that are actually needed and used. That is a huge performance improvement.
    • Improved Console. The new Console is so much better that I’m even considering using CakePHP 2.0 for some of my non-web-based projects. It’s that good!
    • PHPUnit. Previous versions of CakePHP were using SimpleTest framework for unit tests. CakePHP 2.0 switched to the de facto standard PHPUnit. Tests are now easier to write. And integrating CakePHP projects with other Quality Assurance tools should be a breeze.

    There are, of course, more changes. These are just the top few that I am particularly glad about.

    Also, CakePHP 2.0 release is special to me. It’s been a long while since I participated in the development process of an Open Source project. I usually just report bugs and provide help via forums and blog posts. I did more than that with CakePHP 2.0. I actually wrote a couple of patches that were accepted and merged into this release. They were no rocket science, but a contribution nonetheless.

    If you haven’t tried CakePHP before, now is the perfect time to do so. If you have tried older CakePHP versions, you’ll find this one to be much of an improvement. If you have tried it already, please share your thoughts in the comments. Let me know what you think of it.

  3. Pagoda Box – scalable platform for your PHP application

    By Leonid Mamchenkov

    I got my hands on a private beta of Pagoda Box.  It is a platform that you can deploy your PHP applications to.   I gave it a brief look around and I have to say it’s pretty sweet.

    Right after you register and get access to your dashboard, you can add applications.  Applications are cloned from GitHub repositories.  Both public and private repositories are supported.  Once you add an application, you can access it at http://your-app-name.pagodabox.com. If you’d rather have your own domain – you can assign it to your application from the dashboard and all that will remain to be done is adding an A-record in your DNS zone.  Super easy!

    There is more to it, even at this beta stage.  Pagoda Box supports a number of PHP frameworks, including all major ones – CakePHP, CodeIgniter, Lithium, Symfony, Zend, and more.  You can also optionally have a MySQL database for your application.  They even help you out with outgoing email.

    On top of that, you have control as to how many instances of the application you want (the more you have, the more requests you can serve at the same time, and the more you’ll have to pay).  There are statistics of your application performance, requests, and a few other parameters (I’m sure those will grow together with the project).

    I’ll admit, I am too used to hosting my projects on my own servers to take immediate advantage of Pagoda Box.  But I am now seriously considering which projects I can move out of my server and into this platform.  It just makes things so much easier.  Deploying and re-deploying works wonders for any GitHub commit of your project.  Initial resources that one usually needs to try an idea out are free of charge.  If the idea picks up, the prices are more than reasonable (and comparable to other hosting solutions).

    Out of those things that I consider necessary, I haven’t see any mentioning of files (uploaded via application, for example), support for build systems (such as Phing), and some sort of common library of frequently used code (PEAR modules, for example).  But I’m sure that either I simply didn’t look for these hard enough, or they will be added in the future.

    If you are a PHP developer or involved with PHP source on GitHub in any other way, I suggest you try it out.  You can request a private beta invite directly from Pagoda Box website.  Or, if you prefer, I can send you one (I have about 10 of them left for now).  Also watch the demonstration screencasts,  and read through other platform features.

  4. CakePHP GraphViz Models

    By Leonid Mamchenkov

    I have completely and totally rewritten my old script that generates a graph of CakePHP models and their relationships.  Instead of pasting the code in here, I pushed all of its development to GitHub where it now enjoys a new repository.  Please have a look, try it out, and let me know if it does or doesn’t work for you.

    The major changes in this version are:

    • Rewritten as CakePHP Shell instead of being a standalone madness script.
    • Got rid of all dot markup. Utilized Image_GraphViz PEAR package instead.
    • Added support for old and new CakePHP versions (1.2, 1.3, and 2.0).
    • Added option for using only real models (via className property of the relationship) or sticking with the old behavior.
    • Added a bunch of options for tweaking GraphViz output.  And now it’s obvious where to edit them, in case you need more.
    • Improved the styling of the graph a bit – fonts and colors.
    • Improved documentation.

    As a side effect improvement, now that it is a native CakePHP Shell, it’s trivial to add to your project build process.

  5. Disable and enable CakePHP plugins on the fly

    By Leonid Mamchenkov

    I am currently working on a rather large project which is based on CakePHP framework.  In order to simplify the task, I’ve broken down the whole application into a number of CakePHP plugins.  Now, however, I want to enable/disable plugins on the fly.  After a brief search around I couldn’t find how to do that.  Asking a question at #cakephp IRC channel did it.  RabidFire instantly replied with the link that gave me an idea.   30 seconds later I had a working solution.

    CakePHP plugins extend AppController.  So all that one needs to do is add the following lines to app/app_controller.php (Using CakePHP 2.0, but it’s trivial to adopt for earlier versions):

    public function beforeFilter() {
        $allowedPlugins = array('crm', 'articles');
        if (!empty($this->request->params['plugin']) && !in_array($this->request->params['plugin'], $allowedPlugins)) {
            throw new ForbiddenException();
        }
    }
    
  6. ORM Designer

    By Leonid Mamchenkov

    Here is a tool that might help you with your MVC framework, like CakePHP, Symfony, and others – ORM Designer.  In essence, it is a graphical user interface for drawing a visual representation of your project (such as an Entity Relationship Diagram (ERD)) and than converting it into the code.  You can specify which framework and which  ORM you want to use and it will generate the appropriate bits and pieces.  What’s even more interesting is that it has import functionality, which means that you can start using it with an existing project.  Here is the video that shows and explains more.

    Of course, I got excited about it, downloaded and installed.   Two things that disappointed me were:

    1. It’s a native Windows application, which runs on Linux through the wine emulator.  While it works fine, I’d much prefer a native application that I could integrate with the rest of my development environment.
    2. CakePHP import is not supported at this time.

    Other than that though, it looks very promising.  I’ve seen quite a few applications that help with database design, and ORM Designer stands well in that row.  You can create entities, define fields, specify indexes, and associate entities with each other using relationships.  Many-to-many relationships are supported, as are entity inheritance.  While inheritance does make it for a bit more complicated structure of the project (with app/models/base/ folder for CakePHP), it’s very nice to have such support for bigger, more complex projects.

    The project is commercial, with a 14 days evaluation version available for download. If you like it enough to buy, the price is very reasonable – 99 EUR per license.

    Try and see if you like it, and provide some feedback to the guys who are developing it.  ORM Designer has all the chances of becoming an extremely useful tool and since it is still in its early development, your feedback would be of the most value.

  7. Unit tests with CakePHP

    By Leonid Mamchenkov

    I’ve spent a large part of yesterday setting up the testing environment for a CakePHP project.  As always, every time I do something that I have done before, I wanted to do it better, using all the experienced that was acquired previously.  And this often leads to the discovery of new things – both good and bad.  Here is a record of what I’ve learned yesterday.

    Continue reading “Unit tests with CakePHP” »

  8. CakePHP + GraphViz = making sense of a numerous models

    By Leonid Mamchenkov

    NOTE: THIS IS VERY MUCH OUTDATED! Read about the update or go directly to GitHub repository for the new version.

     

    I have a task at hand.  I have to re-introduce myself to a rather large codebase.  It’s a project that migrated to CakePHP a couple of years ago and haven’t seen since.  There was a whole team of people working on it sense then, and now I need to make sense of all those changes that were done and help reorganize and refactor them a bit.  When I looked into CakePHP’s models/ folder, I was surprised to find 50+ models there.  Each and every one of them links to other models.  And documentation is practically non-existing.  How do I go about it?  I hack up a little script to help me out.

    There is a really elegant and beautiful tool for graphing things – GraphViz.  If you haven’t heard of it, you need to drop whatever is that you are doing and familiarize yourself with GraphViz.  Right now.  Right.  This. Second.  You are missing out a whole universe until you do so.  I’ll wait.

    Now that you are back, I just want to mention a very slick tool, which is a part of GraphViz package – dot.  It is a simple language in which you can describe graphs.  Sort of like “A goes to B, which goes to C”.   You specify your graph in a very human readable format in a text file, and then dot will transform that text file into an image format of your choice (PNG, JPEG, GIF, etc).  The primary beauty of this is that those text files can be generated automatically by using pretty much any programming language.

    So here is what I did.  I assumed the following:

    1. Project documentation should be in app/docs/ folder.  That’s where I’ll put the script and that’s where it will generate the dot configuration, dot later will generate the graph of all my models and their relationships.
    2. Main project application folder is app/.  Models are stored in app/models/ folder.
    3. Project can have a number of plugins, which can have their own models, which I still want to know about.  Plugins are in app/plugins/ and if plugin xyz has models, they are stored in app/plugins/xyz/models.
    4. My project is under version control.  Specifically I use Subversion, but it’s easy to adjust the script to support other systems.
    5. I can get current project revision by running a command in shell.  For Subversion that is /usr/bin/svnversion.

    I probably assumed a whole bunch of other things, but you can get an idea of how simple the setup is from the above ones.

    Here is how I generate a graph of all models and their dependencies:

    cd app/docs/
    php -f graph.php > graph.dot
    dot -Tpng graph.dot > graph.png
    

    Obviously, I can’t show you the full graph from that system (it’s not open source, it’s not mine, and it will drive you insane in a matter of seconds), but here is how a small part of that image looks like.

    There is a different colour for each type of model relationship ($belongsTo, $hasMany, and $hasAndBelongsToMany). Each model folder is in a separate sub-graph cluster. There is a legend graph on the image. The current time stamp and version control revision are also imposed on the image for easier referencing.

    And here is the source for the graph.php script. Feel free to modify any way you like. If you spot any major bugs or better ways of doing things, please let me know in the comments.

    Continue reading “CakePHP + GraphViz = making sense of a numerous models” »

  9. CakePHP : Building factories with models and behaviors

    By Leonid Mamchenkov

    CakePHP is a wonderful framework.   Recently I proved it to myself once again (not that I need much of that proof anyway).  The problem that we had at work was a whole lot of code in once place and no obvious way of how to break that code into smaller, more manageable pieces.  MVC is a fine architecture, but it wasn’t obvious to me how to apply it to larger projects.

    In our particular case, we happen to have several data types, which are very similar to each other, yet should be treated differently.  Two examples are:

    1. Client account registrations.   Our application supports different types of accounts and each type has its own processing, forms, validations, etc.  However, each type of account is still an account registration.
    2. Financial transactions.  Our clients can send us money via a number of payment methods – credit cards, PayPal, bank wires, etc.  Each type of the transaction has its own processing, forms, validations, etc.  However, each type of the transaction is still a financial transaction of money coming in.

    Having a separate model for each type of account or for each type of transaction seems excessive.  There are differences between each type, but not enough to justify a separate model.  Having a single model though means that it’ll continue to grow with each and every difference that needs to be coded in.  Something like a class factory design pattern would solve the problem nicely, but the question is how to fit it into an existing MVC architecture.  Read the rest of this post for a demonstration.

    Continue reading “CakePHP : Building factories with models and behaviors” »

  10. Enforcing coding styles in PHP

    By Leonid Mamchenkov

    I came across a plugin for CakePHP which helps to check if the certain code follows CakePHP coding style.  While I haven’t tried it, I think the better way is to utilize CodeSniffer.  As per PHP_CodeSniffer PEAR page:

    PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

    Which basically means that PHP_CodeSniffer is a generic tool for validating your code.  You can use for CakePHP, WordPress, or any other PHP project that you are working on.  The best part is that you can create your own set of rules regarding coding style and then make sure that your team follows it. If you don’t care that much for your own rules, then you can use one of the many existing rulesets.  Some of these come together with CodeSniffer package, others are available on the Web.

    Setting up CodeSniffer for my team at work has been a long lasting TODO item, however it looks like I will be able to start working on this next week.  Once it created, tested, and everyone is happy with it, we’ll have it in the pre-commit hook in our Subversion repository.  This way, we will prevent commits of any code that does not follow our rules.  Of course, I plan to only run CodeSniffer against the code that we wrote in-house.  There is no need to re-format all the third-party code just for the sake of it.  Plus, we are rarely doing any modifications of the third-party code at all.