Tagged: framework RSS Toggle Comment Threads | Keyboard Shortcuts

  • webscriptz 22:16 on 23/06/2011 Permalink | Reply
    Tags: , , framework, , , , , , , , ,   

    YiiFramework adding functions to your models 

    MVC has some principals to it, like the fat model principal that I’ll show in this tutorial and which is used in a lot of frameworks. I’m going to show how to add and use functions that can be added to a model.

    There are a few ways to add to a models that increase code re-usability. One way is to just add methods to the models, methods which can execute complex queries and make your controller thinner.

    The first example, a query that will get all products within a price range, it will need a limit and an offset for pagination, so in the Products model, the following function can be added:

    1
    public function findAllByPrice($min=0, $max=1000000, $offset = 0, $limit = 30) {
    2
        $Criteria = new CDbCriteria();
    3
        $Criteria->condition = "price >= :min AND price <=:max";
    4
        $Criteria->limit = $limit;
    5
        $Criteria->offset = $offset;
    6
        $Criteria->params = array ( ':min' => $min, ':max' => $max );
    7
        return $this->with(array('brand'))->findAll($Criteria);
    8
    }

    Now in any of the controlller files this method can be easily accessed and returns all of the ‘Product’ objects with a single line command:

    1
    $Products = Product::model()->findAllByPrice(100, 700, 0, 30);

    Another way to do the same is to:

    1
    public function getProducts($category){
    2
        return $this->findAll('categoryIdFk=:category', array(':category'=>$category));
    3
    }

    Let’s implement the first query using two named scope methods that I’ll create.

    1
    // Price Between - Named Scope ---------------------------------//
    2
    public function scopePriceBetween($min, $max) {
    3
        $Criteria = new CDbCriteria();
    4
        $Criteria->condition = "$this->tableName().price BETWEEN :min AND :max";
    5
        $Criteria->params = array(':min' => $min,   ':max' => $max);
    6
        $this->getDbCriteria()->mergeWith($Criteria);
    7
        return $this;
    8
    }

    Now an additional named scope for the limit element

    1
    //  Limit - Named Scope --------------------------------------//
    2
    public function scopeLimit($limit = 30, $offset = 0) {
    3
        $Criteria = new CDbCriteria();
    4
        $Criteria->limit = $limit;
    5
        $Criteria->offset = $offset;
    6
        $this->getDbCriteria()->mergeWith($Criteria);
    7
        return $this;
    8
    }

    Now we can simply use our named scopes to get our data back, and then we could continue to pile on named scopes to return our data.

    1
    $Products = Product::model()->scopePriceBetween(0, 5000) ->scopeLimit(30, 1)->findAll();

    The query that actually gets executed is simple and looks like this:

    1
    SELECT * FROM `Product` WHERE Product.price BETWEEN :MIN AND :MAX LIMIT 30 OFFSET 1

    This tutorial is a modified tutorial from another website/blog that unfortunantly isn’t online anymore, domain is being sold by godaddy.com, so I search my archives to retrieve this on, and post him here, hence a few additions, deletes and rewrites from me.

     
  • webscriptz 00:39 on 22/02/2010 Permalink | Reply
    Tags: annoyed, framework, , , , ,   

    Annoyed…with coding 

    I’ve been coding away all week, that is between my other jobs at home :P

    In all my ‘wisdom’ and persistence i made a small app with Yii which stores data in an array and serializes it into a db, and a basic CRUD, a proof of concept to see if the framework was capable of doing this and in the fastest way possible.

    No problem so far, getting it is simple, but apparently you Yii doesn’t permit everything. So you need a $temp variable to store the model data in, suffice to say that it isn’t really the shortest route to take nor in my opinion the fastest bus alas I have to do with it. This little app of not more then a 60 lines of code took me four days, four damn days to figure out the error,  honestly if we have methods to ‘save space’ in your coding why not permit them?

    I’m not going to bother everybody with my nagging so, I’ll stop here.

    For those who want to see the topic on the yii forums.

     
  • webscriptz 23:47 on 20/12/2009 Permalink | Reply
    Tags: , framework, , , , , , ,   

    YII framework configuration 

    I’m toying with the Yii Framework for some time now and even if i cost me a lot of anger and frustration in the beginning I’m starting to like it more and more, alas I do have to say that the documentation isn’t always that clear and for someone who begins or who’ll write some large applications the configuration file can be a hassle so here’s my solution:

    Brake down the configuration file in multiple files, this will give you some speed disadvantage and some will  saying that I’m raping Yii framework purpose for speed but at least to me it seems more clear

    This is the protected.config/main.php

    dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'WEBSITENAME',
    'modules'=>array(
    'users'=>array(
    //sub modules in the module users
    'modules'=>array(
    'messaging',
    'profile',
    'dashboard',
    )
    ),
    'about',
    'forums',
    ),
    // preloading 'log' component active loading
    'preload'=>array('log'),
    
    // autoloading model and component classes lazy loading
    // I make the difference between CformModel and CActiveRecord
    'import'=>array(
    'application.models.*',
    'application.models.forms.*',
    'application.models.database.*',
    ),
    
    // application components
    'components'=>array(
    // enable cookie-based authentication
    'user'=>array('allowAutoLogin'=>true),
    
    // data relinquished to database.php
    // for easy access and usability as also for the future
    // installation procedure, it's less to write to a file
    'db'=>include(dirname(__FILE__).'/database.php'),
    
    // for a better overview we exculded url routes to a seperate file
    'urlManager'=>include(dirname(__FILE__).'/routes.php'),
    
    //authentication component needs data from db for CdbConnection
    'authManager'=>array(
    'class'=>'CDbAuthManager',
    'connectionID'=>'db',
    'defaultRoles'=>array('authenticated', 'guest'),
    ),
    
    //security measures
    'request'=>array(
    'enableCsrfValidation'=>true,
    'enableCookieValidation'=>true,
    ),
    ),
    
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    // uncomment the following if you want static params in the application
    //'params'=>array(include(dirname(__FILE__).'/params.php'))
    );

    database.php

    'CDbConnection',
    'connectionString'=>'mysql:host=localhost;dbname=mysql',
    //'connectionString'=>'pgsql:host=localhost;port=5432;dbname=mysql',
    'username' => 'root',
    'password' => '',
    );
    ?>
    

    routes.php

    'path', // path or get
    'urlSuffix' => '', //.html .whateverextentionyouwant
    'showScriptName' => true,
    'rules'=>array(
    'users/recovery/perimeterSecurity/'=>'users/recovery/perimeterSecurity',
    ),
    );
    ?>
    

    param.php

    //nothing in it at the moment

     
    • Cherry 10:58 on 30/12/2009 Permalink | Reply

      Hi,I’m trying Yii now.Almost everything could be done with Yii.But all the components,those how I configure ,will run.Something ,like,DB session…
      well,my english is so poor!

    • webscriptz 03:32 on 01/01/2010 Permalink | Reply

      I’m a bit torn between speed and programming luxury and because sometimes Yii is poorly documented, nice example code but not all the options are given actually sometimes very few.

      I’m currently doing models and testing them in Yii and i can’t get my head really around it, Codeigniter was really easy to use in the model logic but now what’s the logic, Model driven or controller driven because of the AR layer and the model sql query which seems implemented in the controllers. I thought controllers were just a gateway between view and model because it’s the model that usually contains all the logic not the controller.

  • webscriptz 23:09 on 24/09/2009 Permalink | Reply
    Tags: , framework, , , , , , register,   

    Register & Yii projects 

    So, I think ti’s time for some updates on the website.

    I has been a bit wild the last 3 weeks. School has started yet again and I had a project to finish not to mention that i’m starting to see through the Yii framework structure. I’ve been writing diagrams for different project that I have in my head too and that takes a lot of time. Besides figuring out how I want the database to be and my database system had schifted, alto mysql is the favorite, a friend encouraged that i take a look to postgresql. Yii has given me a hard time to basically because it’s been a bit of a struggle to change coding methodes in function of Yii and then I don’t start talking about the RBAC system it has build-in. It took every neuron to understand everything form begin to end. Models was also something, Yii has two model types and the active records model is something to understand, I was a bit lost at how I would have to write my own AR functions but thanks to the forums I got that figured out too. All in all everything is good at the moment.

    I’ll be uploading some pictures of the system I wrote recently so you can get some sneaky peaks at it but unfortunately it won’t be open source system.

     
  • webscriptz 22:46 on 07/09/2009 Permalink | Reply
    Tags: framework, , , opinion, personal, , , , ,   

    Yii framework very first findings 

    Yii framework (yiiframework.com) is a very fast php5 framework. At first it seems like a jungle in the code, everything is put into motion to make the framework very fast. It’s been really a steep lurning curve, even if you seen oop programming, alas seen it in a totally different way that corresponds to nothing in yii. So you have to adapt to it’s style of coding, writing and thinking.

    It’s not a regular MVC framework, yii had a module concept integrated, if you want to use it that it. Tinny mini applications, pull them out and they don’t work but in the larger application they work just fine.

    The logic as said is a bit different, in the configuration of the app you got one large file that has everything in it, but it’s messy, so i sacrificed a bit of my speed to put it into separate files, separated db config and routes as this gave a more clear picture of the code and you find your environment variables really fast, if you need to switch dbs

    The documentation of yii could be better, some things are left up to your sense, chuncks of code are left out instead of leaving the whole things standing which would be far more handy. Luckily for he who wants to contunie with it there is a good forum with dedicated people answering even the most idiotic questions.

     
  • webscriptz 00:40 on 01/11/2008 Permalink | Reply
    Tags: , framework, , ,   

    Cakephp 

    Begin this week I began to experiment with cakephp but there’s a downside, Cake is something like rails but unfortunately the framework has less good tutorials.

     
  • webscriptz 21:41 on 20/08/2008 Permalink | Reply
    Tags: , build, create, framework, , rails,   

    Restful_authentication 

    This is probably one of the easiest authantication plugins for rails. I’m learning it at the moment and it isn’t easy, the biggest obstacle is the tutorials that often are but available for rails 1.x.x.

    Railscasts – restful_authentication.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel