Skip to content

Instantly share code, notes, and snippets.

MODEL
public function getActieveKanalen($id)
{
$var = $this->kanalen( array("condition"=>"id=".$id) );
$var = array_pop($var);
if(isset($var))
return "Yes";
else
return "No";
@yjeroen
yjeroen / yiirm
Created November 28, 2011 09:56
Yii get attributes of related models
/**
* Returns the attribute of the related HAS_MANY models as a string
* @param object $model->relation
* @param string attribute you want to return
* @param string $break The way you want to seperate the values [comma|enter|array]
* array returns a CHtml::listData array
* @return mixed string or array
*/
public function getRelValues($relatedModels, $attribute, $break='comma')
{
@yjeroen
yjeroen / gist:1400069
Created November 28, 2011 11:29
composite PK like uniqueness
array('field1', 'unique', 'criteria'=>array(
'condition'=>'field2=:field2',
'params'=>array(':field2'=>$this->field2),
)),
@yjeroen
yjeroen / gist:1534128
Created December 29, 2011 13:34
E_STRICT error in gridview
I have a CGridview, with a column that calls a function in the model.
$data->getActieveKanalen($data->id, '.$kanaal->id.', $row)
Today, I turned E_STRICT on, and I got the following PHP error. Before I never saw this, because only E_ALL was on. With only E_ALL, the code works without any problems..
PHP Error
@yjeroen
yjeroen / gist:1534181
Created December 29, 2011 13:47
E_STRICT error in gridview 2
VIEW:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'incident-grid',
'ajaxUpdate'=>true,
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>$columns
));
@yjeroen
yjeroen / gist:1534731
Created December 29, 2011 16:04
dynamic column
VIEW:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'incident-grid',
'ajaxUpdate'=>true,
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>$columns
));
## tr() is a custom trace function that does Yii::trace(CVarDumper::dumpAsString($user),'jlog');
CONTROLLER:
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
@yjeroen
yjeroen / gist:1560242
Created January 4, 2012 14:22
I think I got the solution!
<?php
class foo extends CActiveRecord
{
public function save($runValidation=true,$attributes=null)
{
if( Yii::app()->db->getCurrentTransaction() === null )
{
$transaction = Yii::app()->db->beginTransaction();
$transaction->attachbehavior('modelSaveTransaction', new CBehavior);
class foo extends CActiveRecord
class User extends foo
class Post extends foo
userController User Post
========== === ===
{
function actionThatAddsUserAndPosts
@yjeroen
yjeroen / gist:1633460
Created January 18, 2012 15:15
save relations pattern
# What I want:
# A method that saves related (simple!) models. Simple models only have 1 attribute that requires user input.
# Second thing is that I want relations to be toggled with an "active" attribute.
# What I have:
# If I want a Many Many relation to be 'toggled', i need to make a Has Many relation to the in-between-table Model.
# - Public array which holds all related data that needs to be saved.
# The array is like this:
array
(