Getting unknown property in ActiveQuery Yii 2 - php

Hello I have been getting unknown property error in when I am not using a foreach method of getting the value of the active query instance I need help accessing it without using foreach.
Here is my current code
$status = SiteController::LookupStatus($_user);
<?php if($status->status == 1): //if status == 0?>
<?= HTML::a('<b>REMOVE</b>',
['site/removesubject', 'containerid' => $values['containerid']],
['class' => 'btn-danger btn-transparent' , 'data-method' => 'post']) ?>
<?php else: ?>
<!-- do this -->
<?php endif; ?>
I tried accessing it with $status->status but I am getting the error but when I use the foreach method I am successfully getting it.
Also, here is my active record
public static function LookupStatus($clientid){
return Enrollment::find()->select(['status'])->where([
'clientid' => $clientid,
]);
}

You should specify one(), all() at the end of find() in your model.
public static function LookupStatus($clientid){
$model = Enrollment::find()->select(['status'])
->where(['clientid' => $clientid,])
->one()
return $model->status;
}

Related

In CakePhp, how can I retrieve the value of only one column from my database?

I am new to CakePHP but I have been using PHP for a while. I am trying to create a helper that would provide the level of access of a user (ACL).
Here is my ACLHelper.php so far
<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\ORM\TableRegistry;
class ACLHelper extends Helper{
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id]);
return $acl->first();
}
}
I call this function in my view this way
<?= $this->ACL->getACL($user->id, 'is_items', '4') ?>
And this is the output
{ "is_items": "4" }
What I need is the function to return true or false if the value of the field equals or is higher then the value of $level provided to the function. Now if I do this :
<?= $this->ACL->getACL($user->id, 'is_items', '4')->is_item ?>
it will return just the value. My problem is that I do not want to specify the field twice.
Thanks in advance for any help
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
// Get the first ACL record right here
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id])->first();
// Compare the requested field against the provided level
return $acl->$acl_field >= $level;
}

get input data in controller yii2?

i am trying get input field from a form in yii2. i need to use it in a controller depending on the value. i am trying to see the value using var_dump but it is not working. i am getting "NULL" as the value.. Or is there a way to make a form use different controllers.
controller
public function actionBlog()
{
$thumbs= new Thumbs;
$thumbs->user=Yii::$app->user->identity->email;
$thumbs->topic_id=Yii::$app->getRequest()->getQueryParam('id');
$ra=Yii::$app->request->post('rate');
var_dump($ra);
if(ra=='down'){
if ($thumbs->load(Yii::$app->request->post()) && $thumbs->validate()) {
$thumbs->load($_POST);
$thumbs->save();
return $this->refresh();
}
} else {
return $this->refresh();
}
return $this->render('blog',[
'thumbs' => $thumbs,
]);
}
this is my view
<?php $form = ActiveForm::begin(['id' => "contact-form"
]);
?>
<?= $form->field($thumbs, 'rate')?>
<?= Html::submitButton('Update', ['blog'], ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
i also tired using doing it like this
$rr=Yii::$app->request->post($thumbs)['rate'];
var_dump( $rr);
and i get this error:
Illegal offset type in isset or empty
You have a error in if(ra=='down') condition, '$' is missing.
If I remember right .. you should try
$ra=Yii::$app->request->post(['Thumbs']['rate']);

Rendering partial form

Is there any way to render partial view which contains a part of form that it's main part is in another view file with AJAX?
I exactly mean one form variable:
`<?php $form = ActiveForm::begin(['enableAjaxValidation' => true,]); ?>`
For Example :
Controller
public function actionOlddetform()
{
return $this->renderAjax('_olddet');
}
View
<?php $form = ActiveForm::begin(['enableAjaxValidation' => true,]); ?>
<?= $form->field($model, 'date')->input() ?>
<?= $form->field($model, 'annotations')->textarea(['rows' => 3]) ?>
<div id="details-form"></div>
<?php ActiveForm::end(); ?>
Part of form included with AJAX for details-form container depends on date value. I know how to check date and show any content of that partial view but when I want to include a part of form I get an error:
PHP Notice 'yii\base\ErrorException' with message 'Undefined variable: form'
It seems you forgot to actually pass the model into your view:
public function actionOlddetform()
{
return $this->renderAjax('_olddet', ['model' => $dataModel]);
}
And if you want to render "sub-views" from your main view, you need to pass the variables in there as well (even though I don't see a render call in your view):
<?= $this->render('_formPart', ['form' => $form, 'model' => $model]) ?>

get_class() expects parameter 1 to be object, array given in Yii

i'm just research about Yii for 2 weeks but i have a project . the mission is use Yii Cactiveform to create a form. But I get this error : get_class() expects parameter 1 to be object, array given. This is my Controller:
public function actionPersonal()
{
// Layout
$this->layout = '../layouts/news_style';
// Title
$this->pageTitle = Yii::app()->params['news_title'];
// Description
$this->pageDescription = Yii::app()->params['news_description'];
// Breadcrumbs
$this->breadcrumbs = array(
'My Profile' => array(),
);
$layout = $this->_layout();
return $this->render('my_profile', array(
'user' => Yii::app()->user->getUser(),
'postDoXe' => $layout['postDoXe'],
'newPosts' => $layout['newPosts'],
));
}
my_profile.php
<div class="input-group">
<?php echo $form->label(Yii::app()->user->getUser(),'username'); ?>
<?php echo $form->textField(Yii::app()->user->getUser(),'username'); ?>
</div>
Yii::app()->user->getUser()
returns an array, but you need a model for your form elements, you have given an array for validation
<?php echo $form->textField(Yii::app()->user->getUser(),'username'); ?>
you need to populate a model using id returned from "Yii::app()->user->getUser()" and then use that model in your form
<?php echo $form->textField($model ,'username'); ?>

Yii Collect Data For Two Or More Models ( Get_Class() Expects Parameter 1 To Be Object, Array Given )

I get this error when I call CreateController : "get_class() expects parameter 1 to be object, array given "
Controll/actionCreate() is as follows:
public function actionCreate() {
$model = new Ogrenci;
$model2 =new Adresler;
$this->performAjaxValidation($model, 'ogrenci-form');
$this->performAjaxValidation($model2, 'ogrenci-form');
if (isset($_POST['Ogrenci'],$_POST['Adresler'])) {
$model->setAttributes($_POST['Ogrenci']);
$model2->setAttributes($_POST['Adresler']);
if ($model->save(false) && $model2->save(false)) {
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
else
$this->redirect(array('view', 'id' => $model->ogrenci_id));
}
}
$this->render('create', array( 'model' => $model,'model2' => $model2));
}
create.php:
<?php $this->renderPartial('_form', array(
'model' => array('model'=>$model,'model2'=>$model2),
'buttons' => 'create'));
?>
And _form.php's fields is as follows:
<div class="row">
<?php echo $form->labelEx($model2,'aciklama'); ?>
<?php echo $form->textField($model2,'aciklama'); ?>
<?php echo $form->error($model2,'aciklama'); ?>
</div><!-- row -->
$this->renderPartial('_form', array(
'model' => array(
'model'=>$model,
'model2'=>$model2
),
'buttons' => 'create'
));
code above means that file _form.php will have access to two variables:
$model - array of two elements, and $buttons - string.
So, to get access to first model you have to write $model['model'], second - $model['model2'].
but in this code
<?php echo $form->labelEx($model2,'aciklama'); ?>
<?php echo $form->textField($model2,'aciklama'); ?>
<?php echo $form->error($model2,'aciklama'); ?>
You are trying to access undefined variable $model2. And this should raise respective error.
The thing that error is not got makes me thinking that somewhere before listed code you access variable $model in the similar way, something like this:
echo $form->labelEx($model,'test');
In the above code $model is array(because you passed array). That is why you receive error that object is expected.
So, you should either pass models or access them in a proper way.
I hope this helps.
I solved another problem.Perhaps someone may be in need..
(CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key)
if ($model->save(false) && $model2->save(false)) {
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
else
$this->redirect(array('view', 'id' => $model->ogrenci_id));
}
to
$a=$model2->save(false);
$model->adresler_id=$model2->adresler_id;
if ($a && $model->save(false)) {
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
else
$this->redirect(array('view', 'id' => $model->ogrenci_id));
}

Categories