I am currently trying to pass a variable from the controller to my model. From a previous view I was passed $id in a button.
<a class="btn btn-info" type="button" href="index.php?r=recipient/index&id=<?php echo $data->id; ?>">Manage</a>
From there, I have accessed $id inside my controller and used it. However, now I am creating a function in the model that needs to use that $id. I can't pass it in as a argument because it is the function beforeSave (which updates my database before I've saved a new entry)
This looks like this in the model Recipient.php
// before you save the function here are the things you should do
public function beforeSave()
{
if($this->isNewRecord)
{
// updates the list_id of the individual with the selected ListId
$this->list_id = Recipient::model()->getListId;
}
return parent::beforeSave();
}
Finally, I am trying to create the function getListId in the same model.
public function getListId()
{
// my code here
}
Is there a simple way to pass a variable from the controller to the model to use? Or, is there a way for a function in the model to access a variable passed to the controller?
Add a property to your model and set that property in your controller.
class Model
{
public $var;
... more code ...
}
actionIndex($idList)
{
$model = Model::model()->find(...);
$model->var = $idList;
... more code ...
}
Related
I am building an application in Laravel. I need to pass some data between two methods in two different controllers. Let me explain You,
I have a controller sellerRegisterController which has a method verifySeller,
class sellerRegisterController extends Controller
{
public function verifySeller()
{
//some logic here
}
}
The above veirfySeller method works on some logic and create a varible ID, I want to pass that varible ID data to another method (submitSellerDetails) in different controller (sellerDetailsController).
class sellerDetailsContorller extends Controller
{
public function submitSellerDetails()
{
//some logic here
}
}
In a nutshell I want to know how can I pass data/variable from one method to another in different controllers? Thanks
If I were you, I would define both methods verifySeller() and submitSellerDetails() in the same controller.
Your controller will look like this:
class sellerRegisterController extends Controller
{
public function verifySeller( Request $request )
{
//some logic here
// passing data to another method in the same controller
$this->submitSellerDetails( $request );
}
public function submitSellerDetails( Request $request )
{
//some logic here
}
}
If you don't want to pass $request variable but some other variable instead, you can do something like this:
class sellerRegisterController extends Controller
{
public function verifySeller( Request $request )
{
//some logic here
// passing data to another method in the same controller
$this->submitSellerDetails( $variableContainingYourData );
}
public function submitSellerDetails( $someArgument )
{
//some logic here
}
}
There's another way to handle it. You can define a variable in the in your controller, assign it whatever data you want inside verifySeller() method and then call submitSellerDetails() method and use that variable inside it.
Update:
class sellerRegisterController extends Controller
{
private $someVariable;
public function verifySeller( Request $request )
{
//some logic here
// assign data which you want to access inside submitSellerDetails()
$this->someVariable = 'some data';
// calling submitSellerDetails() method
$this->submitSellerDetails();
}
public function submitSellerDetails()
{
//some logic here
//do whatever you want with $this->someVariable;
}
}
Hope it helps.
I encountered a strange behaviour which I do not understand when using type-hinting in a Laravel Controller. I have a Profile controller, which should call the same function of a service twice based on different parameters. However, when I call the function the second time, the result of the first call is changed.
Here is my code; Two users' profiles should be displayed on one page:
//use statements are omitted for brevity
class ProfileController extends Controller {
protected $compose_profile_service;
public function __construct(ComposeProfileService $compose_profile_service) {
$this->compose_profile_service = $compose_profile_service;
}
public function compare($user1, $user2) {
$user1_profile = $this->compose_profile_service->composeProfile($user1);
$user2_profile = $this->compose_profile_service->composeProfile($user2);
dd($user1_profile); //Problem: This dd() gives me the profile of user2 instead of user1
}
}
class ComposeProfileService {
public function composeProfile($user) {
//Get some stuff from the DB
$profile_data = $user->profile()->get();
return $profile_data;
}
}
Of course, if I dd() $user1 or $user2 above the respective call of the compose function, the right user is provided.
I want to develop proprietary MVC framework in PHP and understand OOP concepts in crystal clear manner. I stuck right here. First look at code snippet....
// main model class
class Model{
protected static $table;
protected static $primary_key;
protected static $conn;
public function __construct()
{
// variable calling from configuration file
global $defalult_database_engine,$connections;
self::dbConnection();
// query to fetch all columns name belo
$query="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA =? AND TABLE_NAME =?";
$stmt=self::$conn->prepare($query);
$stmt->execute(array($connections['mysql']['database'],'users'));
$fields=$stmt->fetchAll(PDO::FETCH_OBJ);
foreach($fields as $field)
{
$fileldname=$field->COLUMN_NAME;
// creating variable name to matching to the tables fields name
// how to set value of this variable via object
$$fileldname;
}
}
And child model is like this
class Users extends Model
{
protected static $table='users';
}
now turn for controller
class UserController extends Controller{
public function __construct(){
}
public function createUser(){
// user model
$user=new Users();
// calling attributes of the table and set their value
$user->name='full name';
$user->user_name='user name';
$user->password='password';
// finally save the value of fields
$user->save();
}
}
I want to work in above fashion. I convert tables' field names to the variable but unable to reference it via its object.... exactly same way in UserController given above. Is there any idea to make it possible? actually I am currently working in Laravel 4.2 and influenced;
No need to retrieve field details from table; instead of retrieving we must take benefit of dynamic instance variable;which can be generated in php like following code,
$user->user_name; // $user_name variable has been created dynamically and bolongs to the $user table
$user->user_name='your user_name'; // value assigned to dynamic instance variable
$user->save();// this function is defined to the model class
and the code for save() of main model should like as follows:
public function save()
{
$datainfo = (array)$this // this assing the array to the $datainfo variable with all dynamic instance variable
// do your manipulation with data received in $datainfo
}
I see you're trying to recreate Laravel's syntax -- sounds like a fun project.
Laravel utilizes PHP's __get() magic method to create an array of model attributes.
Here's the source code for Laravel's __get and getAttribute methods:
public function __get($key)
{
return $this->getAttribute($key);
}
public function getAttribute($key)
{
if (array_key_exists($key, $this->attributes) || $this->hasGetMutator($key)) {
return $this->getAttributeValue($key);
}
return $this->getRelationValue($key);
}
Simple example
I would like to know if there is any possible way that $data could already exist without being set in that method, and if so how to set it.
public function index(){
if(isset($data)){
//how is this possible?
} else if(isset($this->data){
// set in parent::__construct
// ok i'm going to have to set $data in every method in every controller
$data = $this->data;
}
}
additional info
This is my specific problem,
I am using a framework with a controller class which is extended for every controller.
class ControllerBlog extends Controller {}
Every method in every controller perform a few almost identical tasks. Some of these tasks return data within the scope of the method called.
//e.g
public function index(){
$this->loadthis('blog');
$this->loadthat('blog');
$data = $this->get_this('blog');
...
...
$data['title'] = 'blog title';
use_data($data);
}
I would like to move these tasks to Controller class function __construct to limit the amount of code repeated.
<?php
class Controller {
public function __construct() {
//load this and that and return data;
$data = $this->load_and_return_all(get_class($this));
//class level
$this->data = $data;
}
}
is there a way to get the $data variable for use within the scope of the method without the need of adding any additional code to every method of every controller?
class ControllerBlog extends Controller {
public function index(){
//adding this to every method seems silly
$data = $this->data;
// i would like $data to be set in the construct;
}
}
I am developing a joomla 2.5 component where I need to pass data from controller to model. The controller is receiving data from url. I find that controller is getting the value properly. Now I need to move that value to model from controller. From different post I have found a snippet of code for controller like below.
$datevalue = JRequest::getVar('day',$day); //receiving value from view
$item = JRequest::setVar('day',$datevalue); //setting variable
$model =& $this->getModel('WeeklyProgram'); //assign model
$model->setState('dayVar', $item); // assign value for model
The problem is that I don't know how to receive this value 'dayVar' from model. Can anybody help me on this issue? Thanks.
Use following things
In Modal
class CommunityModelCevent extends JCCModel
{
var $membersCount = null;
function getMembersCount($value) {
$this->membersCount = $value // set your value here 15
// Now you can access this variable into model
}
}
In controller
$ceventModel = CFactory::getModel( 'cevent' );
$membersCount = $ceventModel->getMembersCount(15);
You can do like this . First you make get and set function in the model.Second load the model in the controller and simply pass the values to setter function.Example as follows:
updateratings.php---this is my model
class RatingManagerModelUpdateRatings extends JModelLegacy
{
public $data;
public function get_data(){
$data=$this->data;
return $data;
}
public function set_data($data){
$this->data=$data;
}
}
Controller.php
class RatingManagerController extends JControllerLegacy
{
public function save_ratings(){
$tips = JRequest::getVar('tips'); //get data from front end form
$model = $this->getModel('UpdateRatings'); //load UpdateRatings model
$model->set_data($tips); //update setter function of model
$res=$model->get_data(); // retrieve getter function
//print_r($res);
}
}