othy moves to devmoz !
Alright,
This blog will move moves here http://www.devmoz.com/blog/ so please update your bookmarks and feedreaders (Sorry for the inconvenience ). This will probably be the last post here, so see you there 🙂
ConfComponent db Based configuration
Hello, it has been some time now. My apologies for that, I’ve been kinda very busy lately ( whine whine..). But anyway I’ve found sometime to release something, and oh Ma! it is something! heh well, not really. This time I have a configuration component for you. I hear you saying, but cake already has a configuration system. Yes it has, and it’s very nice. really neat. But it’s not db based, meaning the configuration files are stored in files in app/config/. it’s no big deal but in some cases you have to use the db.
So anyway the component is in bakery along with a tutorial. check it out.
PS: If the links say the article doesn’t exist, you gotta wait until a moderator validates them..or write a comment with your email so I can send it to you.
CakePourTous – Voulez vous Caker avec moi ?
Some of you might already know, but I don’t write about anything until it’s official.. 😛
So what was I saying, oh frenchy, yeh..so I started this blog about cakePHP in french.
It was really needed because the community is growing and some people ( they are quite many ) still can’t cross the language border.
It’s called CakePourTous and translates to Cake For Everyone.
The aim of this new Blog is to provide material about cakePHP. Be it news, articles or Tutorials. Maybe a glossary, or even screencasts! anything that can encourage curious minds, help new comers, or clarify advanced stuff for the experienced bakers.
So if you’re looking to refresh your french skills as cakebaker or if you’re straightforwardly a french speaker, I give you a rendez-vous there.
Last but not least, there is the official french cakephp google group jump there if you need some help or want to join the community by helping others.
Cake!
SwiftMailer Component
Based on the really cool SwiftMailer library by Chris Corbyn, a serious alternative to PHPMailer. This Component makes it easy to send views, or wrap a body message with a layout, suitable for newsletter for example, in addition, it embeds the images in the view on the fly, so you don’t have to change your Cake habits ( you use $html->image as you would normally ). you just need to add an attribute to the images you want to send with a embed=”swift”
Hmm what else, oh yea there are some utility functions that make life a bit easier, read it cake-ish.
a note though, this Component doesn’t intend to hide the functionalities of Swift, its syntax is alreay clean, inventing another one is just pointless.
You can download the Swift Mailer Component from bakery. oh yeah the tutorial is there too.
Cake!
othAuth 0.5.2
This is just a minor-bugs-fixing version + support for the newly introduced lazy model loading, as always you can find the component and the helper here. I updated the docs too.
- Fixed a bug in getData ( both in the component and the helper)
- Fixed a bug related to the ‘/’ route
- Added lazy model loading support
The latter affects othAuth because it creates the models within the component.
othAuth 0.5
oh my god it’s been ages since my last post, have never been a fan of blogs to be honest
anyway sorry guys the (huge) delay,
but I have good ( old ) news, othAuth 0.5 is here! and the docs too! yay!
The helper, component and article are all at the bakery
So what’s new ?
othAuth now has the notion : modes, a mode is a way of handing users/groups and permission, the default mode is called oth, in that mode, a user belongs to a group and a group has many belongs to many permissions. another mode is nao, and in which a user has many groups, another mode is acl but it’s in heavy dev. I’m planning to add a sim mode, in which there are no groups, just user s and permissions.
some other features include limit login atempts, a way of doing some action ( blocking access to login page, add a captcha, email administrator ) if a user has too many wrong login attempts.
you can add other info to the auth session, for example the user model hasOne profile, you can choose to have the profile available in the session too, that means the session info now is not limited to user, group and permission.( this feature requires the unbindAll function )
also you can use a different encryption method, even your own, by default othAuth uses md5, bu you can use, crypt, sha1 or a callback.
Keeping track of logins, is another interesting feature to discover
Please, Don’t forget that othAuth is a community thing, even if I wrote it, I don’t really consider it mine, it’s for the cakePHP community, you can put some efforts to understand its code and make it better, document some thing, fix bugs and give back to the community
thanks for the people who helped Naonak in the code, and Dieterbe for the docs.
Cake!
unbind All associations except some
This is a very handy method ( in your AppModel ).
Many times you have a model that has a lot of associations, and for some stuff, you don’t want the overhead of all the associated data. For that you use $this->recursive = ‘-1’, this turns all associations completly. or you can use calls to unbindModel. but then sometimes, you don’t know what associations the model has ( ie. a piece of code you wrote that is used by other people, a component most likely ).
for that there is unbindAll, this method will shut off all associations except the ones passed in params, usage:
$this->Special->Product->unbindAll(array('belongsTo'=>array('Category','Manufacturer'),'hasMany'=>array('Image')));
Neat eh ? here is the code:
function unbindAll($params = array())
{
foreach($this->__associations as $ass)
{
if(!empty($this->{$ass}))
{
$this->__backAssociation[$ass] = $this->{$ass};
if(isset($params[$ass]))
{
foreach($this->{$ass} as $model => $detail)
{
if(!in_array($model,$params[$ass]))
{
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
unset($this->{$ass}[$model]);
}
}
}else
{
$this->__backAssociation = array_merge($this->__backAssociation, $this->{$ass});
$this->{$ass} = array();
}
}
}
return true;
}
.
generateEnumList
as the name suggests, this method ( in your AppModel ) generate a list based on the values of an enum field.
function generateEnumList($fieldName)
{
foreach($this->_tableInfo->value as $field)
{
if($field['name'] == $fieldName) {
$enum = $field['type'];
break;
}
} foreach(split("','", substr($enum, 6, -2)) as $num => $name) { $return[$name] = $name;
} return $return;
}
generateNestedList
UPDATE: 15/03/2007 snippet updated. Before it was only working with tables that has a field named ‘name’ now it supports name, title and the value of Model::displayField
also introduced conditions which makes it a bit incompatible with the old unless you call it without params since a new param has been introduced. If anyone has a tip on how to indent code properly in WP please let me know.
This method does the same thing as generateList, except that it’s used to return a hierachy
its main use is with select tags
NOTE: generateNestedList assumes a parent_id field, and a record with parent_id = 0 is a root field. put the two methods in your AppModel
function generateNestedList($conditions = null,$sort = null,$indent = '--')
{
$showField = ($this->hasField('name'))?'name':(($this->hasField('title'))?'title':$this->displayField);
$this->recursive = '-1';
$cats = $this->findAllThreaded($conditions,array(
$this->name.'.id',$this->name.'.'.$showField,$this->name.'.parent_id'),$sort);
$glist = $this->_generateNestedList($cats,$indent,$showField); return $glist;
}
function _generateNestedList($cats,$indent,$showField,$level = 0)
{
static $list = array();
for($i = 0, $c = count($cats); $i < $c; $i++)
{
$list[$cats[$i][$this->name]['id']] = str_repeat($indent,$level).$cats[$i][$this->name][$showField];
if(isset($cats[$i]['children']) && !empty($cats[$i]['children']))
{
$this->_generateNestedList($cats[$i]['children'],$indent,$showField,$level + 1);
}
}
return $list;
}
Restrict a record from deletion if it has associated records
ex: Category has Many Article
You don't want to delete a Category if it has related Articles how to do that ?
Well the other way around is possible, you just set 'dependent' to true when you define the association:
$hasMany = array('Article'=>array('dependent'=> true));
now a category is deleted along with all its related articles. beforeDelete is called for every article ( to do some app clean up, ie. delete a dir etc..)
You still want to restrict ?
ok here it is:
if(!$this->Category->Article->hasAny(array('Article.category_id'=> $cat_id)))
{
$this->Category->del($cat_id);
}
Gotchas
This is a good work around, Unfortunetly there are cases where it's useless.
Supose we have another Model Called User.
User hasMany Categories
in the definition of User we've set 'dependent' to true,
which means when we delete a User all its categories will be deleted.
OH NO!! you say ? because those categories might have articles and if they are deleted they will leave orphan childs alone :(.
Tragedy, well not really:
function beforeDelete($id)
{ if(!$this->Article->hasAny(array('Article.category_id'=> $id)))
{
return true;
}
return false;
}
Ahhh I see a smile in your face now baby !
we use Category::beforeDelete so if 'dependent' is to true in any Model that hasMany Categories, thet category won't be deleted and we will save orphan childs, how charitable..Eh..just a last thingy, if the other models that hasMany Categories have exclusive set to true, beforeDelete won't be called.
Well what do you want, Black and White.