I am receiving $fb_data[uid] i.e Facebook UID from my model and I want to put this variable's value into my controller where the signup process is taking place
passing fields in controller like this:
$sql_data = array('student_fname' => $student_fname, 'student_sname' => $student_sname, 'student_org' => $student_org, 'student_title' => $student_title, 'student_uid' => ?
I am confused how to insert the Facebook UID value here at my controller..
You need to have a function in your model which returns your $fb_data. In the controller you have (if not already done) to load the model
$this->load->model('model_name');
Then you can access your model function from the controller by calling
$this->model_name->function_name();
The section 'loading a model' in the CI documentation explains it again.
Do like this :
First create one method in controller :
Ex : In controller
function fbcontent() {
$this->load->model('your model name');
$content = $this->model_name->fbaction();
}
and In model
function fbaction(){
$sql_data = array('student_fname' => $student_fname, 'student_sname' => $student_sname, 'student_org' => $student_org, 'student_title' => $student_title, 'student_uid' => ?
return $sql_data;
}
Related
I am using Laravel 5.3 and I would like to make a query in requests file which I made to have some validation rules for a form where a user can edit his channel. In that file I would like to make a query which would look something like this:
$channelId = Auth::user()->channels()->where('id', $this->id)->get();
So that I can get the channel id and exclude it from the rules array, this is how a file looks like:
public function rules()
{
$channelId = Auth::user()->channels()->where('id', $this->id)->get();
return [
'name' => 'required|max:255|unique:channels,name,' . $channelId,
'slug' => 'required|max:255|alpha_num|unique:channels,slug,' . $channelId,
'description' => 'max:1000',
];
}
I am not sure how to get the channel id of that object that is being updated in the requests file?
When inside of a Requestobject, you can access input as #Silwerclaw correctly said by calling $this->input("id") when you have an input with name "id".
When outside of the object, you can use the facade: Request::input("id").
I used model for that matter, in the request file we can access that object by $this and by model name using this we can access all property, So change just as below.
$channel = Auth::user()->channels()->where('id', $this->channel->id))->first();
But I am not doing like this, i am directly use $this->channel->id in rule as below.
return [
'name' => 'required|max:255|unique:channels,name,' . $this->channel->id,
'slug' => 'required|max:255|alpha_num|unique:channels,slug,' . $this->channel->id,
'description' => 'max:1000',
];
I used a session for that matter, I have stored a key in edit function and then retrieved it in the request file in my query like this and now it works, and the user is not able to manipulate it in the form:
$channel = Auth::user()->channels()->where('id', session('channel_id'))->first();
I have to create a form for a set of models, but unfortunately, I don't know how to do.
My first idea is to create a single form and a controller action which renders the view containing the form. But, this idea let me face an error. I create an action like this :
public function actionAddInfo($id){
$participant = Participant::model()->find('id_participant = ' . $id);
$info = InfoComp::model()->findAll('id_event = ' . $participant->id_event);
// here I must save the model if submitted
$this->render('addInfo', array('model' => $info));
}
In fact, the relationship in my models Participant, Evenement is below :
'idEvent' => array(self::BELONGS_TO, 'Evenement', 'id_event');
When accessing the variable $info in the view,
echo count($info);
I got the exception :
Undefined variable $info
This exception let me ask whether it is possible to proceed like that. I need your help. Else, can somebody suggest me another way to proceed ?
You are sending the variable with name model and you are trying to access it with name $info..
All you need to change is this:
$this->render('addInfo', array('info' => $info));
There is a function in AppController:
function products()
{
$products=$this->Product->find('all',array('order' => array('Product.publish_order ASC') ,'fields'=>array('id','name','price','publish')));
$this->set('products',$products);
$counter=$this->Pincode->find('count',array('order' => array('Pincode.product_id DESC '),'conditions' => array('product_id' => $pid,'status'=>0)));
$this->set('counter',$counter);
}
In my layout products.ctp I need to set a $pid (as you see in appcontroller class method I used this) and pass it through.
Another solution is to use the cakephp session
Code to add Value in session :
$this->Session->write('Product.id', 25);
Code to read Value from session :
$pid= $this->Session->read('Product.id');
Code to delete Value from session :
$this->Session->delete('Product.id');
I'm starting developing with Symfony2 and looks like I need help. I have Product entity related with SynchronizationSetting entity. I can edit product data by form maped with his entity. But I also need to modify some data related to product in SynchronizationSetting. To do that I've modified the form so it look like that (Vendor\ProductBundle\Form\ProductType.php):
...
->add('synchronization_setting', 'choice', array(
'choices' => array('daily' => 'Daily', 'weekly' => 'Weekly', 'never' => 'Never'))
After form is submitted selected checkbox values are passed to setSynchronizationSetting method in Product Entity. Then I do that (Vendor\ProductBundle\Entity\SynchronizationSetting.php):
public function setSynchronizationSetting($data)
{
$synchronizationSetting = new SynchronizationSetting();
$synchronizationSetting->setDaily(in_array('daily', $data) ? '1' : '0');
...
}
And now I need to somehow save those SynchronizationSetting entity into database. I read that calling entity manager from here is very bad practice so... how should I save this?
One possible way (I'm not sure if it's good practice)
public function setSynchronizationSetting($data)
{
$synchronizationSetting = new SynchronizationSetting();
$synchronizationSetting->setDaily(in_array('daily', $data) ? '1' : '0');
}
public function retSynchronizationSetting()
{
return $this->synchronizationSetting;
}
Then in your controller in place where you handle form data you call retSynchronizationSetting() and save entity using EntityManager.
I am wondering how I could use data from a Model B while I am validating Model A, here to check if an entered Banknumber is a correct one:
My Users specify their bankaccount during the registration. E.g. the "banknumber". I am validating this the normal way in my user.php model
var $validate = array(
'banknumber' => array(
'minLength' => array(
'rule' => array('minLength', 8),
'message' => '...',
'required' => true,
),
Now I want to know if the entered Banknumber is a real one, so I got a table "Banks" in my DB with all real Banknumbers, and I am using some own validation functions which I specify in app_model.php.
function checkBankExists($data) {
if (!$this->Bank->findByBanknumber($data)) {
return false;
} else {
return true;
}
}
But this is never working, because while I am validating the User-Model, I can only use this one in an app_model - function, accessing it with $this->name or so... a $this->Bank is NOT possible, I get:
Undefined property: User::$Bank [APP\app_model.php
Call to a member function findByBanknumber() on a non-object
Is there ANY way to import/access other models in a function in app_model.php?
Thank you!
ClassRegistry should generally be used instead of AppImport as AppImport only loads the file, rather than registering it properly, cake style.
Using the example above.
$bnk = ClassRegistry::init('Bank');
$bnk->findByBanknumber($data);
you can import your model, create instance of it and use it as you like:
App::import('model','Bank');
$bnk = new Bank();
$bnk->findByBanknumber($data);