How can configure two tables for admin module? - php

I have a custom module. I am using this module to upload images through admin. I have 6 more form fields in my module. I am currently using one table to store values of these form fields. However as like in magento, i am planning to store image values in another table. My table structure is given below.
Table : Banner
Fields : banner_id( primary_key , int(11) )
banner_name( varchar (250) )
banner_count( small_int (6) )
status( small_int (6) )
store_id( varchar (250) )
Table : banner_images
Fields : bi_id( primary_key , int (11) )
banner_id( int (11) ) //this should be the 'banner_id' of banner which holds this imge
bi_name( varchar (255) ) //stores image_name
My current edit file look like this:
<?php
class Karaokeshop_Banner_Block_Adminhtml_Banner_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = 'banner';
$this->_controller = 'adminhtml_banner';
$this->_updateButton('save', 'label', Mage::helper('banner')->__('Save Banner'));
$this->_updateButton('delete', 'label', Mage::helper('banner')->__('Delete Banner'));
}
public function getHeaderText()
{
if( Mage::registry('banner_data') && Mage::registry('banner_data')->getId() )
{
return Mage::helper('banner')->__("Edit Banner");
}
else
{
return Mage::helper('banner')->__('Add Banner');
}
}
}
My sql file look like this :
?php
$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$this->getTable('banner')};
CREATE TABLE {$this->getTable('banner')} (
`banner_id` int(11) unsigned NOT NULL auto_increment,
`banner_name` varchar(255) NOT NULL default '',
`banner_count` smallint(6) NOT NULL default '0',
`status` smallint(6) NOT NULL default '0',
`store_id` varchar(255) NOT NULL default '',
`img` varchar(255) NOT NULL default '',
PRIMARY KEY (`banner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
How can I achieve this? Please provide links, if there is any tutorials for this. Thanks in advance

Related

Wordpress - save data from own plugin

My own wordpress plugin generates this data of a file:
filename|creator|date
This is my filename|Max Mustermann|20.11.2017
This is my filename|Anne Mustermann|26.11.2017
This is my filename|Alex Mustermann|27.11.2017
How is wordpress built to save this data? How do I do that the right way?
Thanks for your answers!
Lingo
Try this example,
function bot_install()
{
global $wpdb;
$table = $wpdb->prefix."bot_counter";
$structure = "CREATE TABLE $table (
id INT(9) NOT NULL AUTO_INCREMENT,
bot_name VARCHAR(80) NOT NULL,
bot_mark VARCHAR(20) NOT NULL,
bot_visits INT(9) DEFAULT 0,
UNIQUE KEY id (id)
);";
$wpdb->query($structure);
// Populate table
$wpdb->query("INSERT INTO $table(bot_name, bot_mark)
VALUES('Google Bot', 'googlebot')");
$wpdb->query("INSERT INTO $table(bot_name, bot_mark)
VALUES('Yahoo Slurp', 'yahoo')");
}
You get the result.

CakePHP table relation $belongsTo show no recors in drop down menu in add action

I have simple ralation tables : Each table (Agrement) 'umowas' have one item from table (Adult) 'rodzics' and one item from table (Children) 'dziecis'.
When call myaddress.com/cakephp/Umowas/add i do not see drop down menu for item 'rodzic_id' and 'dzieci_id' with its items names. I see only numer.
For example drop down menu for 'rodzic_id' shows Array(1=>1, 2=>2, 3=>3), not names of its record, like array( 1=>'Item name 1', 2=> 'Item name 2').....
Debug.log and Error.log is empty.
My query at bottm of screen is:
Nr Query Error Affected Num. rows Took (ms)
1 SELECTRodzic.idFROMakademia-uat.rodzicsASRodzicWHERE 1 = 1 2 2 3
2 SELECTDzieci.idFROMakademia-uat.dziecisASDzieciWHERE 1 = 1 1 1 2
My code is:
<?php
class UmowasController extends AppController {
var $name = 'Umowas';
var $scaffold;
var $helpers = array('Form' );
function index() {
$this->Umowa->recursive = 1;
$umowas = $this->Umowa->find('all');
$this->set('umowas', $umowas);
}
}
?>
-----------------------------------------
<?php
class RodzicsController extends AppController {
var $name = 'Rodzics';
var $scaffold;
function index() {
$this->Rodzic->recursive = 1;
$rodzics = $this->Rodzic->find('all');
$this->set('rodzics', $rodzics);
}
}
-------------------------------------------------------
<?php
class DziecisController extends AppController {
var $name = 'Dziecis';
var $scaffold;
function index() {
$this->Dzieci->recursive = 1;
$dziecis = $this->Dzieci->find('all');
$this->set('dziecis', $dziecis);
}
}
------------------------------------------
<?php
class Umowa extends AppModel {
var $name = 'Umowa';
public $belongsTo = array(
'Rodzic' => array(
'className' => 'Rodzic',
'conditions' => '',
'order' => '',
'foreignKey' => 'rodzic_id' //or your external key
),
'Dzieci' => array(
'className' => 'Dzieci',
'conditions' => '',
'order' => '',
'foreignKey' => 'dzieci_id' //or your external key
)
);
var $helpers = array('Form' );
function add() {
if (!empty($this->data)) {
$this->Umowa->create();
$this->Umowa->save($this->data);
$this->redirect(array('action'=>'index'));
}
$rodzics = $this->Umowa->Rodzic->generateList();
$this->set('rodzics', $rodzics);
$dziecis = $this->Umowa->Dzieci->generateList();
$this->set('dziecis', $dziecis);
}
}
?>
--------------------------------------------------------
<?php
class Rodzic extends AppModel
{
var $name = 'Rodzic';
var $hasMany = 'Umowa';
}
?>
-------------------------------------------------------
<?php
class Dzieci extends AppModel
{
var $name = 'Dzieci';
var $hasMany = 'Umowa';
}
?>
---------------------------------------------------------
view/Rodzics/add and do not exists
view/Umowas/add do not exists
view/Dziecis/add do not exist
`
My Tables is like this:
`
CREATE TABLE IF NOT EXISTS `dziecis` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`imie_dziecka` varchar(18) DEFAULT NULL,
`nazwisko_dziecka` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
CREATE TABLE IF NOT EXISTS `rodzics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`imie_rodzica` varchar(18) DEFAULT NULL,
`nazwisko_rodzica` varchar(15) DEFAULT NULL,
`telefon` int(9) DEFAULT NULL,
`adres_email` varchar(50) DEFAULT NULL,
`ulica` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `umowas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`warunek_umowy` varchar(2048) DEFAULT NULL,
`data_rozwiazania_warunku` date DEFAULT NULL,
`data_zawarcia_umowy` date DEFAULT NULL,
`data_zerwania_umowy` date DEFAULT NULL,
`nr_umowy` int(4) DEFAULT NULL,
`rok_szkolny` int(4) DEFAULT NULL,
`wizerunek` varchar(3) DEFAULT NULL,
`miesiac_rozpoczecia_platnosci` int(2) DEFAULT NULL,
`raty` varchar(3) NOT NULL,
`utworzono` date NOT NULL,
`modyfikacja` date NOT NULL,
`zabezpieczona` varchar(3) NOT NULL,
`wydrukowana` varchar(3) NOT NULL,
`dzieci_id` int(11) NOT NULL,
`rodzic_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`status_umowy_id` int(11) NOT NULL,
`miasto_id` int(11) NOT NULL,
`klasa_lub_grupa_dziecka_id` int(11) NOT NULL,
`cennik_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
I have translate all tables and names in project to english. Also done job with command line cake bake, and all tables started to works and display fields. Problem was in models, and not declared public $displayField.
Everyboday have to do scafolding command line tool and have english names, that a lot of easy. And most importand is to declare $displayField and point it to title, or name field in each tables.
Thanks all for support.

trying to create a 'join' statement using Zend DB Table - zend framework 2

I have a model that needs to execute a join query on 2 tables... lets call them friend_list and user_profile.
I am having a heck of a time trying to put together the zend style code to produce the proper query I need to accomplish this... below is the desired query:
SELECT friend_list.friend_id, user_profile.id, user_profile.username
FROM `friend_list`
INNER JOIN `user_profile`
ON friend_list.friend_id = user_profile.id
where user_id = 1
Here is my model_friends
<?php
//model created to add user to database, sendmail etc...
require_once 'Zend/Db/Table/Abstract.php';
class Model_Friends extends Zend_Db_Table_Abstract
{
protected $_name = "friend_list";
public function fetchFriendList($userID)
{
$accountsTable = array('up' => 'user_profile');
$select = $this->select()
->from($this->_name)
->join($accountsTable, 'up.id = friend_List.friend_id', array())
->where("up.id = ?", $userID);
$result = $this->fetchAll($select);
if ($result !== null){
echo $select;
return $result;
} else {
echo "no records found";
}
}
}
the above model produces the follow SQL statement which is NOT what I want...
SELECT `friend_list`.*
FROM `friend_list`
INNER JOIN `user_profile`
AS `up`
ON up.id = friend_List.friend_id
WHERE (up.id = '1')
adding the table structures as requested:
DROP TABLE IF EXISTS `buzz`.`friend_list`;
CREATE TABLE `buzz`.`friend_list` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`friend_id` int(11) NOT NULL,
`approved_timestamp` date NOT NULL,
`status` varchar(15) DEFAULT 'pending',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `buzz`.`user_profile`;
CREATE TABLE `buzz`.`user_profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mob` varchar(50) NOT NULL DEFAULT 'no info',
`dob` varchar(50) NOT NULL DEFAULT '',
`yob` varchar(50) NOT NULL DEFAULT '',
`language` varchar(75) NOT NULL DEFAULT 'English',
`gender` varchar(25) NOT NULL DEFAULT 'no info',
`about` varchar(1000) NOT NULL DEFAULT 'no info',
`country` varchar(45) NOT NULL DEFAULT 'no info',
`username` varchar(45) NOT NULL,
PRIMARY KEY (`id`,`username`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
Try changing your Zend_Db_Select object to the following:
$select = $this->select()
->join($accountsTable, 'friend_list.friend_id = user_profile.id', array())
->where('user_profile.id = ?', $userID)
->reset('columns')
->columns(array('friend_list.friend_id', 'user_profile.id', 'user_profile.username'));
This is not an answer to the question but since i cant comment yet i will post this here. I found the following website helpful with the join examples.
github.com
the end result of my model_friends script is as follows:
<?php
//model created to add user to database, sendmail etc...
require_once 'Zend/Db/Table/Abstract.php';
class Model_Friends extends Zend_Db_Table_Abstract
{
protected $_name = "friend_list";
public function fetchFriendList($userID)
{
$select = $this->select()
->from($this)
->setIntegrityCheck(false)
->join(array('u'=>'user_profile'), 'friend_list.friend_id =u.id', array())
->columns(array('u.id', 'u.username'))
->where("friend_list.user_id = ?", $userID);
$result = $this->fetchAll($select);
if ($result !== null){
echo $select;
return $result;
} else {
echo "no records found";
}
}
}

Trying to post info to database via wordpress but just showing up as '0'

I have a wordpress theme and i'm trying to add an additional field into the themes backend (it's for a gallery slider that has a description and caption text box, I'm trying to add a "quote" box).
As I'm a novice with databases, I've found the relevant files and copied all references to "description" and changed the word to "quote". I also added a "Quote" column into my table.
Everything appears to work fine, however when I type in text to the input field in the backend and click save, it changes the text to "0"
Does anyone know why this might be, what have I done wrong?
I think this is the code that posts to the DB....like I say though I'm a noob with DB's
add_action('wp_ajax_list_slider_items', 'list_slider_items');
function list_slider_items()
{
global $wpdb;
$result = $wpdb->get_results("SELECT IMAGEID, TYPE, CONTENT, THUMB, CAPTION, DESCRIPTION, QUOTE, WIDTH, HEIGHT FROM {$wpdb->prefix}backgrounds WHERE GALLERYID='".$_POST['GALLERYID']."' ORDER BY SLIDERORDER");
$i=0;
foreach($result as $row)
{
echo getSliderItemImage($row->IMAGEID, $row->TYPE, $row->CONTENT, stripslashes($row->CAPTION), stripslashes($row->DESCRIPTION), stripslashes($row->QUOTE), $row->THUMB, $row->WIDTH, $row->HEIGHT);
$i++;
}
die();
}
add_action('wp_ajax_save_slider_items', 'save_slider_items');
function save_slider_items()
{
global $wpdb;
for($i=0; $i<count($_POST['imageID']); $i++)
{
$wpdb->update($wpdb->prefix.'backgrounds', array('CAPTION'=>$_POST['CAPTION'][$i], 'DESCRIPTION'=>$_POST['DESCRIPTION'][$i], 'QUOTE'=>$_POST['QUOTE'][$i], 'SLIDERORDER'=>($i+1)), array('IMAGEID'=>$_POST['imageID'][$i]), array('%s', '%s', '%d'), array('%d'));
}
die();
}
And this looks liek the code that created the table:
function create_backgrounds_table(){
global $wpdb;
$prf = $wpdb->prefix;
$create_query = <<<EOT
CREATE TABLE `{$prf}backgrounds` (
`IMAGEID` int(11) NOT NULL auto_increment,
`GALLERYID` bigint(20) unsigned NOT NULL,
`SLIDERORDER` int(11) unsigned NOT NULL,
`EXT` varchar(255) NOT NULL,
`CAPTION` text,
`DESCRIPTION` mediumtext NOT NULL,
`QUOTE` mediumtext NOT NULL,
`TYPE` varchar(20) default NULL,
`CONTENT` text,
`THUMB` text,
`WIDTH` int(11) default NULL,
`HEIGHT` int(11) NOT NULL,
PRIMARY KEY (`IMAGEID`),
KEY `GALLERYID` (`GALLERYID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
EOT;
$create = $wpdb->get_results($create_query);
}

set relation grocery crud error

I have 2 tables
CREATE TABLE `tbl_patient` (
id_patient INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(25) NOT NULL DEFAULT "not available",
att1 VARCHAR(5 ) NOT NULL DEFAULT "att1",
att2 VARCHAR(25) NOT NULL DEFAULT "att2",
att3 VARCHAR(25) NOT NULL DEFAULT "att3",
CONSTRAINT `uc_Info_patient` UNIQUE (`id_patient`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
and
CREATE TABLE `tbl_patient_medicine` (
id_patient_medicine INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_patient INTEGER NOT NULL,
name_medicine VARCHAR(50) NOT NULL DEFAULT "",
dosis VARCHAR(50) NOT NULL DEFAULT "",
start_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
treatment VARCHAR(50) NOT NULL DEFAULT "",
times_per_day VARCHAR(50) NOT NULL DEFAULT "",
CONSTRAINT fk_ID_Patient_Medicine FOREIGN KEY (id_patient) REFERENCES `tbl_patient`(id_patient)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
As you can see table patient_medicine is the intermediate table of between tbla_medicines, and table patient.
Now I want to consult all data from tbl_patient_medicine with grocery crud like in this sqlfiddle
supossing I pass the id in the uri (in the example will be id_patient=1)
I have
public function details_medication($my_id = 0)
{
try{
$crud = new grocery_CRUD();
$crud->where('id_patient',$my_id);
$crud->set_table('tbl_patient_medicine');
//HOW TO DO IT?
$crud->set_relation('id_patient', 'tbl_patient', 'id_patient');
$output = $crud->render();
$this->_output($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
SO I have tried different ways but got errors like this:
A Database Error Occurred
Error Number: 1052
Column 'id_patient' in where clause is ambiguous
SELECT `tbl_patient_medicine`.*, j7a675883.id_patient AS s7a675883
FROM (`tbl_patient_medicine`)
LEFT JOIN `tbl_patient` as j7a675883
ON `j7a675883`.`id_patient` = `tbl_patient_medicine`.`id_patient` WHERE `id_patient` = '1' LIMIT 25
Line Number: 87
I did your example:
Controller:
function medicine()
{
$crud = new grocery_CRUD();
$crud->set_table('tbl_patient_medicine');
$crud->required_fields('id_patient','name_medicine','dosis','start_date','treatment','time_per_day');
$crud->columns('id_patient','name_medicine','dosis','start_date','treatment','time_per_day');
$crud->fields('id_patient','name_medicine','dosis','start_date','treatment','time_per_day');
$crud->set_relation('id_patient','tbl_patient','name');
$output = $crud->render();
$this->_example_output($output);
}
It works!
Edited:
$crud->set_relation('id_patient','tbl_patient','{name}, {att1}, {att2}, {att3}');
Try by changing WHERE j7a675883.id_patient

Categories