How to use two models in one view - Joomla 3 - joomla

I want to create component based on com_weblinks.
This component will display categories and links on single page.
In 3.0 I don't understand how I can use 2 models (categories models and links model) in one view.

First approach
I did this by modifying the controller as follows (this is the controller for user)
function doThis(){ // the action in the controller "user"
// We will add a second model "bills"
$model = $this->getModel ( 'user' ); // get first model
$view = $this->getView ( 'user', 'html' ); // get view we want to use
$view->setModel( $model, true ); // true is for the default model
$billsModel = &$this->getModel ( 'bills' ); // get second model
$view->setModel( $billsModel );
$view->display(); // now our view has both models at hand
}
In the view you can then simply do your operations on the models
function display($tpl = null){
$userModel = &$this->getModel(); // get default model
$billsModel = &$this->getModel('bills'); // get second model
// do something nice with the models
parent::display($tpl); // now display the layout
}
Alternative approach
In the view directly load the model:
function display($tpl = null){
// assuming the model's class is MycomponentModelBills
// second paramater is the model prefix
$actionsModel = JModel::getInstance('bills', 'MycomponentModel');
}

Related

how to hide id from url in Laravel 6?

hide id from url
https://wallpaperaccess.in/photo/162/download-wallpaper
i want url like this
https://wallpaperaccess.in/photo/download-wallpaper
ImagesController.php
public function show($id, $slug = null ) {
$response = Images::findOrFail($id);
$uri = $this->request->path();
if( str_slug( $response->title ) == '' ) {
$slugUrl = '';
} else {
$slugUrl = '/'.str_slug( $response->title );
}
$url_image = 'photo/'.$response->id.$slugUrl;
//<<<-- * Redirect the user real page * -->>>
$uriImage = $this->request->path();
$uriCanonical = $url_image;
if( $uriImage != $uriCanonical ) {
return redirect($uriCanonical);
}
Route
// Photo Details
Route::get('photo/{id}/{slug?}','ImagesController#show');
NOTE: i don't have any slug column in database, so can we use title as slug?
You should add a column field slug and auto-generate it from title
use Illuminate\Support\Str;
$slug = Str::slug($request->input('title'), '-');
In Models\Image.php
public function getRouteKeyName()
{
return 'slug';
}
In routes\web.php
Route::get('photo/{image:slug}','ImagesController#show');
In app\Http\Controllers\ImagesController.php
use app\Models\Image.php;
...
public function show(Image $image)
{
// controller will automatically find $image with slug in url
// $image_id = $image->id;
return view('your.view', ['image' => $image]);
}
In order to use a slug in the URL instead of an id, you'll need to...
Create a column in your table where you store the slug. A good way to make a slug unique is to append the actual id at the end. If you really don't want to see the id anywhere, you have no choice, you'll have to ensure the slug is unique yourself (there are a lot of ways to achieve this).
This is one way to automatically create an unique slug:
Make sure the slug column is nullable, then open your model and add these methods.
They are called "model events".
created is called when the model is, well, created.
updating is called when you are updating the model but before it's actually updated.
Using created and updating should automatically create the slug when you create or update a Images instance.
protected static function booted()
{
parent::booted();
static::created(function (Images $images) {
$images->slug = Str::slug($images->title.'-'.$images->id);
$images->save();
});
static::updating(function (Images $images) {
$images->slug = Str::slug($images->title.'-'.$images->id);
});
}
From a SEO point of view, updating the slug when the title change is arguably not a good practice, so you might want to omit this part (static::updating...), it's up to you.
Go to your model and add the following method:
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'slug'; //or whatever you call the slug column
}
This way, the router will resolve your model by the slug, not the id.
In your route file, remove the id and change the name of the slug to match the name of your model:
Route::get('photo/{images}','ImagesController#show'); //here I'm assuming your model is Images from what I see in your controller
In your controller, change the declaration of your show method to this:
public function show(Images $images)
{
dd($images);
// if you did all this correctly, $images should be the Images corresponding to the slug in the url.
// if you did something wrong, $images will be an empty Images instance
//
//
// your code...
}
Images should be renamed to Image, models should not be plural. However, it should not make any difference here.

How to access Model from Views in CodeIgniter

How to access Model from Views in Codeigniter.
I have a Model with a function, i need to call this function from view
Please Read the CI documentation First:
If You are using a MVC Structured Framework then you should be following the way it works.
You Need the Controller:
public function your_controller_function(){
// To load model
$this->load->model ('your_model');
//To Call model function form controller
$data = $this->your_model->model_function($ex_data);
//TO Send data to view
$this->load->view('your_view',['data'=>$data]);
}
Inside your model:
public function model_function($ex_data){
// Your Querys
// user return to send you query result to the controller anything
// Sample Example of query and return
$query = $this->db->select('*')
->where('your_column',$data['column_name'])
->get('your_table');
$your_result = $query->row_array();
if($this->db->affected_rows() > 0){
return your_result;
} else {
return FALSE;
}
}
Inside your view you can write :
<?php
$this->load->model('your_model');
$this->your_model->some_function();
?>
For example in User_controller load User Model $this->load->model('Users_model');
Then User View page $viewpage = this->Users_model->somefunction($id);

Form validation in yii

I trying to post form in yii but don't have any idea regarding validation, i go through some yii documentation but not getting it. can't we do validation without form object of yii? means in view i am using normal HTML for form of yii.
Validation is built into Yii in Models, whether it is Form models or CActiveRecord models.
In order to implement validation, place the validation rules in your model. In the example below, I am using an activerecord model.
class Customer extends CActiveRecord {
// :
public function rules(){
return array(
array('name, surname, email', 'required'),
array('age, email', 'length','min'=>18)
);
}
You can now validate ANY form, whether you are using Yii forms or plain HTML forms.
To enforce validation, your controller must populate the model values, then call upon the model to check the data against the rules you defined earlier.
class CustomerController extends CController {
// :
$customerModel = new Customer;
// Set fields using this format ...
$customerModel->attributes['name'] = $_FORM['user'];
// ...or this ...
$customerModel->age = $_FORM['age'];
// ...of this
$customerModel->setEmail($_FORM['email'];
// Now validate the model
if ($customerModel->validate) {
return true;
}
else {
return false;
}
// :
}
}
In action, you need to add
$this->performAjaxValidation($model);
in _form, add
'enableAjaxValidation'=>true,
and in model, you need to set rules,
public function rules(){
return array(
// array('username, email', 'required'), // Remove these fields from required!!
array('email', 'email'),
array('username, email', 'my_equired'), // do it below any validation of username and email field
);
}
I think, this will be helpful for you.

Multiple models - no data retrieved

I am trying to use multiple models in my view. My view is a single items and the models are lists displayed within the view using tabs.
in my controller I added the setModel in the display function
class ComplianceControllerCompliance extends JControllerForm
{
public function display( $cachable = false, $urlparam = array() )
{
$view = $this->getView( 'myview', 'html' );
$view->setModel( $this->getModel( 'mymodel' ), true );
$view->setModel( $this->getModel( 'content' ) );
$view->display();
}
In my view I try to access the needed information like this
$this->content_items = $this->get( 'Items', 'content' );
$this->content_pagination = $this->get( 'Pagination', 'content' );
$this->content_state = $this->get( 'State', 'content' );
but I dont get any information.
What am I doing wrong?
The basis of working with multiple models is finding a way to get an instance of each model. The most likely issue is that $this->getModel('yourmodelname') returns false.
Test that this part works:
$testModel = $this->getModel('yourmodelname');
var_dump($testModel);
I guess you have already studied the Using multiple models in an MVC component article.

Joomla component : one view calling multiple models?

I want a view to call 2 different models for use.
Controller.php
class StatsController extends JController {
function display()
{
if( !JRequest::getVar( 'view' ) ) {
JRequest::setVar('view', 'stats' );
}
parent::display();
}
...
...
}
Stats view : (index.php?option=com_stats&view=stats)
class StatsViewStats extends JView
{
function display($tpl = null)
{
$model_helpdesk = & JModel::getInstance('Helpdesk','StatsModel');
//$model_chart = & JModel::getInstance('Chart','StatsModel');
//$model_chart = &$this->getModel('Chart');
var_dump($model_chart);
...
...
parent::display($tpl);
}
}
Problem : getting the Helpdesk model works fine, but getting the Chart model either returns a blanc page , or returns null in var_dump. How can i get this second model for use (without modifying the controller) ??
In your controller, you'll need to do the following:
$view = &$this->getView('Stats', 'html');
$view->setModel($this->getModel('Stats'), true);
$view->setModel($this->getModel('Chart'));
$view->setModel($this->getModel('Helpdesk'));
$view->display();
Then you can access each model using the following:
$chartModel = $this->getModel('Chart');
$helpdeskModel = $this->getModel('Helpdesk');
Source

Resources