Result of findBySQL in Yii - php

Query "SELECT * FROM uzytownik" in phpMyAdmin gives me all recodrs from uzytkownik table. But this same query applied in yii controller gives me olny one (first) record. What is wrong?
class StronaController extends CController
{
public function actionIndex()
{
$model = new Uzytkownik;
$wynik = $model::model()->findBySQL('SELECT * FROM uzytkownik');
for($i=0;$i<count($wynik);$i++)
{
echo count($wynik).' '.$wynik ->imie.'<br>';
}
}
}
Output:
1 Jan
Query with WHERE conditions gives me also one record, but it should gives me three.
class StronaController extends CController
{
public function actionIndex()
{
$model = new Uzytkownik;
$wynik = $model::model()->findBySQL('SELECT * FROM uzytkownik WHERE imie=:imie',array(':imie'=>'Jakub'));
for($i=0;$i<count($wynik);$i++)
{
echo count($wynik).' '.$wynik ->imie.'<br>';
}
}
}
Output: 1 Jakub
class Uzytkownik extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}

It should be
$wynik = $model::model()->findAllBySQL('SELECT * FROM uzytkownik WHERE imie=:imie',array(':imie'=>'Jakub'));
Know the difference between,
findBySql() And findAllBySql()

Related

laravel 4 hasOne/ hasMany / belongsTo doesn't work

I have some models belong to Activity Model.
in my Activity.php I had
<?php
class Activity extends \Eloquent {
protected $fillable = [];
public function activity_car_nums()
{
return $this->hasMany('ActivityCarNum');
}
public function newables()
{
return $this->hasMany('Newable');
}
public function serial_codes()
{
return $this->hasMany('SerialCode');
}
public function applys()
{
return $this->hasMany('Apply');
}
}
and in SerialCode.php, I had
<?php
class SerialCode extends \Eloquent {
protected $fillable = ['code'];
public function activity()
{
return $this->belongsTo('Activity');
}
}
and in my controller, when I wrote
$serial_codes = [];
while(count($serial_codes) < $serial_code_total)
{
$code = substr(md5(uniqid(rand(), true)),0,5);
$serial_code = new SerialCode(['code' => $code]);
if(!in_array($serial_code, $serial_codes))
{
$serial_codes[] = $serial_code;
}
}
$activity->serial_codes()->saveMany($serial_codes);
it works.
But when it turns to
//this can get activity object
$activity = Activity::find($id);
//this can get the serial codes of the object above.
$serial_codes = SerialCode::whereActivityId($id)->get();
//this don't work, it returns null
$serial_codes = $activity->serial_codes;
for I really don't know why...
Can anybody help me please, and sorry for my poor English. Thank You.
(If you need any code else please tell me.)
my model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
class Product extends Model
{
public $table="s_products";
protected $primaryKey = 'product_id';
public function reviews()
{
return $this->hasMany('App\ProductReview');
}
public function attributes()
{
return $this->hasMany('App\AttributeMapping');
}
public function images()
{
return $this->hasMany('App\Image');
}
}
I found the reason that why my model query cannot work, because the "_" in function name.
just change
public function serial_codes()
to
public function serialcodes()
then everything will go fine.
Thank to everybody.

Laravel 5.2 Nested Eloquent relationships

I have a User which is of type Player and has several Equipments
I want to request a piece of Equipment and see if the User is it's owner before returning it to the user. If they do not own it they will get an unauthorized response
Here are the relationships I have for the models:
App\User.php
class User extends Authenticatable
{
protected $table = 'user';
public function player()
{
return $this->hasOne(Player::class);
}
}
App\Player.php
class Player extends Model
{
protected $table = 'player';
public function equipment()
{
return $this->hasMany(Equipment::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
App\Equipment.php
class Equipment extends Model
{
protected $table = 'equipement';
public function player()
{
return $this->belongsTo(Player::class);
}
}
EquipmentController.php
With my attempt which is working... just very ugly.
class EquipmentController extends Controller
{
public function show($id)
{
$equipment = Equipment::find($id);
if ( ! $equipment ) {
return 'Equipment does not exist');
}
// my attempt
$test = Equipment::with('player.user')->findOrFail($id);
if ($test->toArray()['player']['user']['id'] != Auth::user()->id){
return 'Unauthorized';
}
//
return $equipment;
}
}
Is there a neater way to do this?
I want something readable in the controller like:
if(!$equipment->ownedBy(Auth::user())){
return 'Unauthorized';
}
Or something similarly as readable.
And once the relationship is found, I'm not sure where the logic should be placed. Should it be in the Equipment model?
Any help would be much appreciated!
In your Equipment model:
public function authorized()
{
return ($this->player->user->id == auth()->user()->id())
}
Then from your controller, try:
$equipment->authorized() //returns true or false

Laravel5.1 query relationships(Eloquent ORM)

My model is Patient->Sample->Ready_Sample ,relationship all is oneToMany ,
My question is I query Ready_Sample need to know patient.name
Patient_Model
class Patient_Model extends Base_Model {
protected $table = 'patients';
public function samples(){
return $this->hasMany('App\Models\Sample_Model','patient_id','code');
}
}
Sample_Model
class Sample_Model extends Base_Model{
protected $table = 'samples';
public function patient(){
return $this->belongsTo('App\Models\Patient_Model','patient_id','code');
}
public function ready_samples(){
return $this->hasMany('App\Models\Ready_Sample_Model','sample_id','code');
}
}
Ready_Sample_Model
class Ready_Sample_Model extends Model{
protected $table = 'ready_samples';
public function sample(){
return $this->belongsTo('App\Models\Sample_Model','sample_id','code');
}
}
In Sample_Controller
class Sample_Controller extends Controller{
public function query(Request $request){
$result = Sample_Model::with(['patient']);
->orderBy("updated_at","desc")
->Paginate(15)
->toJson();
return $result;
}
In Sample I know to get patient.name ,but Ready_Sample how to get Patien.name?
You can get the patient.name using the below code:
$readySample = Ready_Sample_Model::first(); // fetch the first record
echo $readySample->sample->patient->name;
Hope it helps!

Codeigniter: Simple echo of data from DB

What is the best way to create my model so I can call it from my controller like this?
$this->model_model->function->dbname
I have the following model but its rubbish:
Model:
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
Update:
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
Why would you want to? Why not call it like this?:
$this->model_model->functionname('dbname');
A complete skeleton of a model is like this:
<?php
class Mymodel extends Model
{
function get_some_entries($number_rows)
{
return $result = $this -> db -> query("SELECT id, name
FROM tablename
LIMIT $number_rows");
}
}
?>
Best way? You can probably read on CodeIgniter general guide and alter to your situation. But the basic idea is that, you create the model class then load from your controller. Then just call the model function accordingly. For example,
class Custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
...
...
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
}
<?php
class CustomController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('custom_model', 'fubar');
}
public function index()
{
$result = $this->fubar->systemName('dbname');
print_r ($result);
}
}
?>

filter some data(row) in zend view

I have
1.Table:user(userId,userName,userGroup)
2.Model:userModel
3.usercontroller
there i a simple code:
Controller:
class UserController extends Zend_Controller_Action
{
public function getuser()
{
$userModel = new userModel();
$this->view->usergroup = $userModel;
}
}
Model:
class Model_UserGroupModel extends Zend_Db_Table_Abstract {
public function getuser(
{
$select = $this->select();
return $this->fetchAll($select);
}
}
view:
please tell me what code I must insert in view to only have user with specific row like user with group teacher also i use partialoop???
foreach ($this->usergroup->toArray() as $userGroup) {
echo $userGroup['row'];
}
should work
To filter rows based on a particular field like userGroup you need to do a query with where condition.
$where = $this->getAdapter->quoteInto("userGroup = ?", 'teacher');
$select = $this->select()->where($where);
So now you can get users for a particular user group.

Categories