How to get value from table and display it at view? - php

I'm new to PHP framework (using Yii framework). How to get a value in MySQL then display it at a view? I'm confused in defining a model, controller, and how to use it.
Model:
public $name;
public $info;
$product = product::find()->orderBy('name')->all();
public function tablename()
{
return 'productdata';
}
Controller:
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
public function LoadModel($id){
$model=productdata::model()->find($id);
return $model;
}

Have you tried any tutorials?
[models/MyModel.php]
class MyModel extends CActiveRecord {
public function rules() {
return ['id, name, value', 'safe'];
}
}
[controllers/MyController.php]
class MyController extends CController {
public function actionView($id) {
$model = MyModel::model()->findByPk($id);
$attributes = Yii::app()->request->getParam('MyModel');
if ($attributes) {
if ($model->save()) {
$this->redirect('myController/admin');
} else {
throw new CHttpException(500, 'Model not saved. Use echo CActiveForm::validat($model);');
}
}
$this->render('view', ['model' => $model]);
}
}
[views/MyController/view.php]
<?php echo $model->name; ?>

Related

How can I get the variable in the model for the controller?

I have a $send variable in my model. I want to use it on the controller. But I get an error. How can I call it to the controller?
Model:
public function register_user($send)
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return true;
} else {
return false;
}
Controller:
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$this->users->register_user();
$this->load->view('signup', $this->send);
}
You can declare a private variable, say send in your model and make getter and setter in your model class in an encapsulated way to get the value in the controller, like below:
Snippet:
Model:
<?php
class Yourmodel extends CI_Model{
private $send;
function __construct() {
parent::__construct();
$this->send = [];
}
public function register_user($send){
if($this->emailVerify()) {
$this->send = array(
'tipo' => 'Error.'
);
return true;
}
return false;
}
public function setSendValue($value){
$this->send = $value;
}
public function getSendValue(){
return $this->send;
}
}
Controller:
<?php
class Controller extends CI_Controller{
private $send;
public function __construct(){
parent::__construct();
$this->send = [];
}
public function register(){
$this->load->model('users');
if($this->users->register_user()){
$this->send = $this->users->getSendValue();
}
$this->load->view('signup', $this->send);
}
}
Replace your code modal and controller as follows:
You need not to declare $send in modal definition, as you are not passing any value while calling the same modal function.
modal positive return can be array $send itself
Catch value of modal function
Modal :
public function register_user()
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return $send;
} else {
return false;
}
Controller:
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$send = $this->users->register_user();
//print_r($send); // You will get data here
$this->load->view('signup', $this->send);
}
Model
public function register_user($send = "")
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return $send;
} else {
return false;
}
Controller
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$sendRes = $this->users->register_user(); //now you can use this $send response variable
$this->load->view('signup', $this->send);
}

Echoing in Model View Controller

I am new to MVC and I need to echo data from my database in View. Can anyone help me with that? Here is the code:
Controller:
class Index extends Controller {
function __construct() {
parent::__construct();
//echo 'We are in index';
}
function index(){
$this->view->render('index/index');
}
function get(){
$this->model->get();
}
}
Model:
class Index_Model extends Model {
public function __construct()
{
parent::__construct();
}
function get()
{
$sth = $this->db->prepare('SELECT * FROM data');
$sth->setFetchMode(PDO::);
$sth->execute();
$sth->fetchAll();
}
}
How to echo in View?

How to make dynamic attributes in Yii2 like in Laravel?

In Laravel I can write
class MyModel extends Model {
public function getTextAttribute($value) {
return strtoupper($value);
}
}
And then
$model = MyModel::find(1);
$model->text = 'test';
echo $model->text; // returns 'TEXT'
Can I do similar in Yii2?
Add a public var in your MyModel and you can redifine the text getter this way
class MyModel extends Model {
public $text;
public function getText() {
return strtoupper($this->text);
}
}
.
$model= MyModel::findOne(1);
$model->text = 'test';
echo $model->text;

Yii 2 search in 2 many to many relation

I have these models :
class Article extends \yii\db\ActiveRecord
{
.....
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('rel_tag_article', ['article_id' => 'id']);
}
}
class News extends \yii\db\ActiveRecord
{
.....
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('rel_tag_news', ['news_id' => 'id']);
}
}
class Tag extends \yii\db\ActiveRecord
{
.....
public function getRelTagArticles()
{
return $this->hasMany(RelTagArticle::className(), ['tag_id' => 'id']);
}
public function getRelTagNews()
{
return $this->hasMany(RelTagNews::className(), ['tag_id' => 'id']);
}
}
And in the controller
class ArticleController extends \yii\web\Controller
{
public function actionArticle($id_article)
{
$article = User::find($id_article);
...... here ....
return $this->render('article');
}
}
Under ...here... I have to find the news that have common tags with my actual article. What is the right way?
## You can use article active record to get tags and in turn use tags to get news ##
class ArticleController extends \yii\web\Controller
{
public function actionArticle($id_article)
{
$article = User::find($id_article);
$tags=$article->tags;
$related_news=array();
foreach($tags as $tag) {
$related_news=array_merge($tag->getRelTagNews(),$related_news);
}
return $this->render('related_news');
}
}
Visit this yii2 page

Complete Newbie at Laravel, wondering if I'm using models correctly

So I've got three models, "Incomplete", "User", and "Collaboration". They are related with foreign keys as such:
Collaboration->incomplete_id
incomplete_id->Incomplete
Incomplete->user_id
user_id->User
If I want to get the email of a user for a Collaboration model I have the following code
User::find(Incomplete::find($collab->incomplete_id)->user_id)->email);
I feel like this is wrong as I'm not joining any tables but I don't want to break out of mvc and call straight up SQL from the controller. Basically I'm curious how I could do this correctly.
Collaboration Model
class Collaboration extends Eloquent{
//Set the database to connect to as form d
protected $connection = 'formd';
//Set the table for the model to incomplets
protected $table = 'collaborations';
//Set the fillable columns
protected $fillable = array('incompid', 'link', 'shareFlag');
private $rules = array(
'link'=>'required|unique:collaborations|size:56',
'incompid'=>'required|integer'
);
private $errors;
public function validate($data)
{
// make a new validator object
$v = Validator::make($data, $this->rules);
// check for failure
if ($v->fails())
{
// set errors and return false
$this->errors = $v->errors();
return false;
}
// validation pass
return true;
}
public function errors()
{
return $this->errors;
}
public function getId(){
return $this->getKey();
}
public function incomplete(){
return $this->hasOne('Incomplete');
}
}
Incomplete Model
class Incomplete extends Eloquent{
//Set the database to connect to as form d
protected $connection = 'formd';
//Set the table for the model to incomplets
protected $table = 'incompletes';
//Set the fillable columns
protected $fillable = array('name', 'data', 'userid');
private $rules = array(
'name' => 'required|min:3',
'data' => 'required'
);
private $errors;
public function validate($data)
{
// make a new validator object
$v = Validator::make($data, $this->rules);
// check for failure
if ($v->fails())
{
// set errors and return false
$this->errors = $v->errors();
return false;
}
// validation pass
return true;
}
public function errors()
{
return $this->errors;
}
public function getId(){
return $this->getKey();
}
public function getData(){
return $this->data;
}
public function getName(){
return $this->name;
}
public function user(){
return $this->hasOne('User');
}
}
You can use Eloquents relations:
http://laravel.com/docs/eloquent#relationships
class Collaboration extends Eloquent {
public function incomplete()
{
return $this->hasOne('Incomplete', 'incomplete_id', 'id');
}
}
You can then get the data from the incomplete record by doing:
$collab->incomplete->user_id
In this case, use a relation on your models:
class Collaboration extends Eloquent {
public function incomplete()
{
return $this->belongsTo('Incomplete', 'incomplete_id');
}}
And
class Incomplete extends Eloquent {
public function user()
{
return $this->belongsTo('User', 'user_id');
}}
Then, do this:
Collaboration::find($id)->incomplete()->user()->email;
first of all you'll have to update parts of your model class
Class Collaboration extends Eloquent{
protected function incomplete(){
return $this->belongsTo('Incomplete');
}
}
Class Incomplete extends Eloquent{
protected function collaboration(){
return $this->hasOne('Collaboration');
}
protected function user(){
return $this->belongsTo('User');
}
}
Class User extends Eloquent(){
protected function incomplete(){
return $this->hasOne('Incomplete');
}
}
Then inorder to get what your want, here is the query
Collaboration::find($id)->incomplete()->user()->email;

Categories