Can I use two tables on CodeIgniter's model? - mysql

I want do receive data from my table "users" and to insert other data into the table "olders", however I only know how to access it from this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Eixoxy_model extends CI_Model {
protected $table = 'users';
protected $table_id = 'id';
public function __construct() {
parent::__construct();
}
public function getMarketing(){
$mkt = $this->db->get_where('users', array('area' => 'marketing'));
return ( $mkt->num_rows() > 0 ) ? $mkt->result_array() : false;
}
This is what I'd like to insert on other table:
$this->Eixoxy_model->create($xy);
$xy = $this->input->post(); //I want to somehow insert $xy into table "olders"

It is possible to add another table simply by including:
protected $table2 = 'test';

Related

Larvel Eloquent Model save function is not saving but not error

My problem at the moment is that I want to save some values to the database but the don't get saved and I don't get an error..
Both, either $product-save(); or $product-update(array(...)) are not working and I cannot tell why.. My ASIN Model looks fine and is filled with the right fillable attributes...
You guys know why it isn't working?
My Laravel Version: Laravel Framework 5.5.36
This is my class so far:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\ASIN;
class CheckPrice extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'post:CheckPrice';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
$product = ASIN::find(1410);
$product->price = "HELLO";
$product->amountSaved = "HELLO";
$product->percentageSaved = "HELLO";
$product->url = "HELLO";
$product->imgUrl = "HELLO";
$product->save();
//$product->update(array("price" => "HELLO", "amountSaved" => "HELLO", "percentageSaved" => "HELLO", "url" => "HELLO", "imgUrl" => "HELLO"));
$this->printProduct(ASIN::find(1410));
}
My ASIN Model so far:
namespace App;
use Illuminate\Database\Eloquent\Model;
class ASIN extends Model
{
protected $connection = 'facebook_amazon';
public $table = "ASINS";
protected $fillable = [
'ASIN',
'price',
'amountSaved',
'percentageSaved',
'category',
'url',
'imgUrl',
'showApp'
];
}
Kind regards and Thank You!
Use this in the handle methode
$product = App\ASIN::find(1410);
Or while impoting ASIN model use this if you want to keep the handle methode same
use App\ASIN as ASIN;
Use Laravel logs:
if(!$product->save()){
foreach($products->errors() as $error){
Log::info($error);
}
}
Hope this help.

How to display attributes from an object using my_controller in codeigniter

I want to be able to display attributes from a table anywhere in my application and have them be edited by the user. This means the data will come from a row in a database. Each column has a default value which can be changed by the user if they so choose.
The table columns may look like this:
| id | admin_id | practitioner | practitioners | student | students | and so on ...
Here is my model:
class Lang_trans_model extends CI_Model {
public function get_lang_trans($admin_id){
$this->db->where('admin_id', $admin_id);
$query = $this->db->get('lang_trans');
return $query->row();
}
Here is my MY_Controller:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('key_items_model');
$this->load->model('lang_trans_model');
foreach ($this->key_items_model->get_key_items() as $result){
$this->label[$result->key] = $result->value;
}
//$admin_id = $this->session->userdata('admin_id');
$admin_id = 3;
$lang = $this->lang_trans_model->get_lang_trans($admin_id);
//not sure what should go here????
//I want to be able to use $lang->practitioner; to be able to have practitioner, or the edited attribute to show on the page
var_dump($lang);
// produces the following:
//object(stdClass)[26]
//public 'id' => string '3' (length=1)
//public 'admin_id' => string '3' (length=1)
//public 'practitioner' => string 'blah' (length=4)
//public 'practitioners' => string 'blahplural' (length=10)
How do I finish the MY_Controller to be able to use these data fields anywhere in my application?
I do not mind using object oriented syntax or array syntax. Whichever is easier. I just can not figure out how to display the fields without using a foreach statement in every view. I want to call the data with the $lang->practitioner; format in the view.
Thanks in advance.
Declare parent class properties. Than call it from child class:
class MY_Controller extends CI_Controller
{
public $lang;//parent variable declared
public function __construct()
{
parent::__construct();
$this->load->model('key_items_model');
$this->load->model('lang_trans_model');
foreach ($this->key_items_model->get_key_items() as $result) {
$this->label[$result->key] = $result->value;
}
//$admin_id = $this->session->userdata('admin_id');
$admin_id = 3;
//dedicating value to variable
$this->lang = $this->lang_trans_model->get_lang_trans($admin_id);
}
}
Child controller:
class Practitioners extends MY_Controller
{
public function __construct()
{
parent::__construct();
// no need to call model if it is called in parent controller
}
public function index()
{
var_dump($this->lang);
}
}

Zend Framework: Select query

I'm newbie on Zend Framework, I created DbTable , my primary key is id , and my table name is user:
<?php
class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
protected $_name = 'user';
protected $_primary = 'id';
}
after that ,I reviewed Abstract.php(Db/Table/Abstract.php) and I found out that I had to use insert(array $data), so I created a model: (Register.php)
<?php
class Application_Model_Register
{
public function CreateUser($array)
{
$dbTableUser = new Application_Model_DbTable_User();
$dbTableUser -> insert($array);
}
}
and finally , in my Controllers , I created IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$register = new Application_Model_Register();
$register-> CreateUser(array(
'username'=>'test'));
}
}
It works correctly , but I have no idea about Select, How I select query from user table?
our controller should have to be like below
<?php
class IndexController extends Zend_Controller_Action
{
public function getdataAction()
{
$register = new Application_Model_Dbtable_Register();
$register->getListOfUser();
}
}
Now your model should have to be like this,
<?php
class Application_Model_DbTable_Register extends Zend_Db_Table_Abstract
{
protected $_name = 'user';
protected $_primary = 'id';
public function getListOfUser()
{
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$select = $db->select()
->from($_name,array('*'))
->where(1);
$data = $db->query($select)->fetchAll();
return $data;
}
}

Laravel 4 many to many relationship does not display the related data

I'm working with the following models: Game, User and Player where Player extends User. In this case Game and Player have a relationship of N:N and User extends Confide. So code stands like:
app/models/Game.php
<?php
class Game extends Eloquent {
protected $guarded = array();
protected $table = 'users';
public $timestamps = false;
public function players(){
return $this->belongsToMany('Player');
}
}
app/models/User.php
<?php
use Zizaco\Confide\ConfideUser;
class User extends ConfideUser {
protected $table = 'users';
public $timestamps = false;
protected $hidden = array('password');
//Here go original Confide functions
}
app/models/Player.php
<?php
class Player extends User{
public function games(){
return $this->belongsToMany('Game');
}
}
I've populated the pivot table called game_player. As player extends user, pivot table is like this:
game_player
id
game_id
user_id
But if I run this super simple example I have an error:
$game = Game::find($gameId);
$player = $game->players(); //Error in this line
Error
Call to undefined method Illuminate\Database\Query\Builder::players()
I'm confused because I'm not having the error about pivot table was not found so I could think relationship is created fine.
On the other hand if I var_dump games var I obtain:
var_dump($game)
object(Game)[181]
protected 'table' => string 'games' (length=5)
//more var info about DB
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'guarded' =>
If relationship would be created fine... shouldn't relations var have any data?
Thanks!
Just pointing out somethings that could solve the problem:
Your Game.php has the wrong table name.
When you get players, the line should be:
$game = Game::with('players')->find($gameId);
$player = $game->players;

Insert Command in Zend Framework using DBTable

I am new in Zend Framework
This is my DBTable
class Application_Model_DbTable_Employee extends Zend_Db_Table_Abstract
{
protected $_name = 'tab_employee';
}
This is my Models
public function InsertEmployees($array){
$tblEmployee = new Application_Model_DbTable_Employee();
$tblEmployee->insert($array);
}
This is my controller
public function AddEmployeeAction(){
$request = $this->getRequest();
$params = $request->getParams();
$emp = new Application_Model_InsEmployee();
$emp->InsertEmployees(array(
'Name' => $params['name'],
'Date' => $params['date']
));
}
Anybody knows what is the error of this code because it always return an application error . Thanks for advance
In your DbTable do this:
public function InsertEmployees($array){
$this->insert($array);
}
So your DB will look like this:
class Application_Model_DbTable_Employee extends Zend_Db_Table_Abstract
{
protected $_name = 'tab_employee';
public function InsertEmployees($array){
$this->insert($array);
}
}
Then from the controller, create the DbTable instance and "bypass" the model.
$model = new Application_Model_Db_Table();
$model->InsertEmployees($data)