I'm new to Kohana and I'm following this excellent tutorial. I am getting this cryptic error message
ErrorException [ 8 ]: Array to string conversion ~
SYSPATH\classes\Kohana\Log\Writer.php [ 81 ]
after trying to load this url
http://localhost/kohana-blog/index.php/article/new
The problem seems to be originating from my Model_Article() because I get this error with only this line of code in it
public function action_new()
{
$article = new Model_Article();
/*
$view = new View('article/edit');
$view->set("article", $article);
$this->response->body($view);
*/
}
I uncommented database and orm in application/bootstrap.php as suggested by the author.
Here is application/classes/Controller/Article.php:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Article extends Controller {
public function action_index()
{
$view = new View('article/index');
$this->response->body($view);
}
// loads the new article form
public function action_new()
{
$article = new Model_Article();
$view = new View('article/edit');
$view->set("article", $article);
$this->response->body($view);
}
// save the article
public function action_post()
{
$article_id = $this->request->param('id');
$article = new Model_Article($article_id);
// Populate $article object form
$article->values($_POST);
// Saves article to database
$article->save();
// Redirects to article page after saving
$this->request->redirect('index.php/article');
}
}
Here is application/views/article/edit.php
<?php defined('SYSPATH') or die('No direct script access.'); ?>
<h1>Create new article</h1>
<?php echo Form::open('article/post/'.$article->id); ?>
<?php echo Form::label("title", "Title"); ?>
<br />
<?php echo Form::input("title", $article->title); ?>
<br />
<br />
<?php echo Form::label("content", "Content"); ?>
<br />
<?php echo Form::textarea("content", $article->content); ?>
<br />
<br />
<?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>
And here is application/classes/Model/article.php
<?php defined ('SYSPATH') or die('No direct script access');
class Model_Article extends ORM {
}
This is a known issue when running PHP 5.4.12 and beyond. It has been fixed in 3.3.1.
Related
I used the same code (below) in both front and backend, but the pagination is not working in the admin side.
====================MODEL=PART===========================
defined('_JEXEC') or die;
jimport('joomla.application.component.modellist');
class CiieModelOrders extends JModelList
{
public function getItems()
{
// Invoke the parent getItems method to get the main list
$items = parent::getItems();
return $items;
}
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select('title');
$query->from('q2b7v_menu');
return $query;
}
}
====================VIEW=PART===========================
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
class CiieViewOrders extends JView {
protected $state;
protected $item;
protected $form;
protected $params;
public function display($tpl = null) {
$items = $this->get('Items');
$pagination = $this->get('Pagination');
$this->items = $items;
$this->pagination = $pagination;
parent::display($tpl);
}
}
==================TEMPLATE=PART=========================
<?php
JHtml::_('behavior.keepalive');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
//Load admin language file
$lang = JFactory::getLanguage();
$lang->load('com_ciie', JPATH_ADMINISTRATOR);
?>
<div>
<table>
<?php
foreach($this->items as $item){
echo "<tr><td>".$item->title."</td></tr>";
}
?>
</table>
<?php echo $this->pagination->getListFooter(); ?>
</div>
This works fine in the front end (site side).
Session output-> [orders] => stdClass Object ( [ordercol] => [limitstart] => 0 )
Link html/url (for next button)-> <a title="Next" href="/NewJoomla/index.php/component/ciie/?view=other&start=20" class="pagenav">Next</a>
I put the same code in Admin side (backend), and it shows all the paginatiion buttons and everything. But the buttons don't function at all. They simple take me to the top of the page. When I check the links (for example 'next' button) I see this:
Next
(As you can see href attribute value is empty(#).)
Session output-> [orders] => stdClass Object ( [ordercol] => )
(Here also 'limitstart' value doesn't exist at all.
I tried this in different fresh Joomla installations too but the same problems repeats again.
Is there any thing I missed?
Finally I sorted it out! It was a silly mistake!
In the template, I didn't put the list content inside a <form> tag. getListFooter() function shows the pagination buttons, but when clicked the action is not submitted anywhere. I corrected the code as shown below and it worked.
==========TEMPLATE=PART====================
...
<div>
<form action="<?php echo JRoute::_('index.php?option=com_ciie&view=orders'); ?>" method="post" name="adminForm">
<table>
<?php
foreach($this->items as $item){
echo "<tr><td>".$item->title."</td></tr>";
}
?>
</table>
<?php echo $this->pagination->getListFooter(); ?>
</form>
</div>
Thank you all.
controller
userdetails.php
class Controller_Userdetails extends Controller {
public function action_index() {
$view = new View('userdetails/index');
$this->response->body($view);
}
public function action_add() {//load adddetails.php
$userdetails = new Model_Userdetails();
$view = new View('userdetails/adddetails');
$view->set("userdetails", $userdetails);
$this->response->body($view);
}
public function action_post() {//save the post data
$userdetails_id = $this->request->param('id');
$userdetails = new Model_Userdetails($userdetails_id);
$userdetails->values($_POST);
$userdetails->save();
$this->request->redirect('index.php/userdetails');
}
}
views
adddetails.php
<?php echo Form::open('userdetails/post/'.$userdetails->id); ?>
<?php echo Form::label("first_name", "First Name"); ?>
<?php echo Form::input("first_name", $userdetails->first_name); ?>
<br />
<?php echo Form::label("last_name", "Last Name"); ?>
<?php echo Form::input("last_name", $userdetails->last_name); ?>
<br />
<?php echo Form::label("email", "Email"); ?>
<?php echo Form::input("email", $userdetails->email); ?>
<br />
<?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>
I am trying to insert the data into database.The name and email field are loading correctly,if i enter values and hit enter it is redirecting to other page but objects not saved in database.Need help to solve this.
I sometimes find problems in saving while using
$userdetails->values($_POST);
try using
$userdetails->first_name = $this->request->post('first_name');
$userdetails->last_name= $this->request->post('last_name');
$userdetails->email= $this->request->post('email');
Assuming the model is an ORM model (class Model_Userdetails extends ORM {}), you should use ORM::factory() to load the model. Furthermore it's recommended to use the expected parameter to be sure only the values you want to be inserted are used.
$user_details = ORM::factory('Userdetails');
$user_details->values($this->request->post(),
array('first_name', 'last_name', 'email')
);
$user_details->save();
I am working on a basic application using the CI framework.
I have the following error:
404 Page Not Found
The page you requested was not found.
Posted below are my code files.
My Controller code:
class Contact extends CI_Controller{
function _Contact(){
parent::CI_Controller();
}
/*function main(){
$this->load->model('contact_model');
$data = $this->books_model->general();
$this->load->view('books_main',$data);
}*/
function input(){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('contact_model');
if($this->input->post('mysubmit')==true){
$this->contact_model->entry_insert();
}
$data = $this->contact_model->general();
$this->load->view('contact_input',$data);
}
}
Then in Model I have the following code:
class contact_model extends CI_Model{
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
function entry_insert(){
$this->load->database();
$data = array(
'name'=>$this->input->post('title'),
'address'=>$this->input->post('author'),
'year'=>$this->input->post('year'),
);
$this->db->insert('contact',$data);
}
function general(){
$data['base'] = $this->config->item('base_url');
$data['name'] = 'Name';
$data['address'] = 'Address';
$data['year'] = 'Year';
$data['years'] = array('2007'=>'2007',
'2008'=>'2008',
'2009'=>'2009');
$data['forminput'] = 'Student Registration';
$data['fname'] = array('name'=>'name',
'size'=>30
);
$data['faddress'] = array('name'=>'address',
'size'=>30
);
return $data;
}
}
Finally, my View:
<html>
<head>
</head>
<body>
<div id="header">
<?php $this->load->view('contact_header'); ?>
</div>
<?php echo heading($forminput,3) ?>
<?php echo form_open('books/input'); ?>
<?php echo $name .' : '.
form_input($fname).br(); ?>
<?php echo $address .' : '.
form_input($faddress).br(); ?>
<?php echo $year .' : '.
form_dropdown('year',$years).br(); ?>
<?php echo form_submit('mysubmit','Submit!'); ?>
<?php echo form_close(); ?>
<div id="footer">
<?php $this->load->view('contact_footer'); ?>
</div>
</body>
</html>
Can any one please help me?
Remove this in your Contact Controller:
function _Contact(){
parent::CI_Controller();
}
Replace with this:
function __construct(){
parent::__construct();
}
And in your Contact Model remove this:
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
Replace it with this:
function __construct(){
parent::__construct();
$this->load->helper('url');
}
Hey,
I often had similar Problems. Usually I check the following:
Check the Config file if the correct siteroot is set. I often had live server stuff in there.
Check in the .htaccess if the RewriteBase is set to the correct directory.
to make sure its reading the correct value an echo base_url(); (if this works its usually the .htaccess rewrite base).
Hope it helps.
r n8m
[enter link description here][1]
[1]: http://ellislab.com/forums/viewthread/105880/
hmc
If you have developed your application on Windows, you might have not set first character of Controllers and Models name as a capital character.
like /controllers/home.php to /controllers/Home.php
On Linux file names are case sensitive.
Note:- This is a solution to a possible problem. There may be issues of mis-configuration, server and path variables.
Possible Duplicate: CodeIgniter "The page you requested was not found." error?
I'm making a simple MVC component for joomla following the hello world tutorial for the most part, with some some text fields and an image.
The text fields save but the "file" field does not, any ideas?
**Controller:**
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
/**
* MJob Controller
*/
class MJobsControllerMJob extends JControllerForm
{
}
**Model:**
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
class MJobsModelMJob extends JModelAdmin
{
public function getTable($type = 'MJob', $prefix = 'MJobsTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_mjobs.mjob', 'mjob', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_mjobs.edit.mjob.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
}
**view.html.php:**
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
class MJobsViewMJob extends JView
{
public function display($tpl = null)
{
// get the Data
$form = $this->get('Form');
$item = $this->get('Item');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign the Data
$this->form = $form;
$this->item = $item;
// Set the toolbar
$this->addToolBar();
// Display the template
parent::display($tpl);
}
protected function addToolBar()
{
JRequest::setVar('hidemainmenu', true);
$isNew = ($this->item->id == 0);
JToolBarHelper::title($isNew ? JText::_('COM_MJOBS_MANAGER_MJOB_NEW') : JText::_('COM_MJOBS_MANAGER_MJOB_EDIT'));
JToolBarHelper::apply('mjob.apply');
JToolBarHelper::save('mjob.save');
JToolBarHelper::save2new('mjob.save2new');
JToolBarHelper::cancel('mjob.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
}
}
**VIEW (tmpl/edit.php):**
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
echo "<b>MJOB EDIT START</b><br>";
?>
<form action="<?php echo JRoute::_('index.php?option=com_mjobs&layout=edit&id='.(int) $this->item->id); ?>"
method="post" enctype="multipart/form-data" name="adminForm" id="mjob-form">
<fieldset class="adminform">
<legend><?php echo JText::_( 'COM_MJOBS_MJOB_DETAILS' ); ?></legend>
<ul class="adminformlist">
<?php foreach($this->form->getFieldset('details') as $field): ?>
<li><?php echo $field->label;echo $field->input;?></li>
<?php endforeach; ?>
</ul>
</fieldset>
<div>
<input type="hidden" name="task" value="mjob.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
I'm guessing you're using Joomla! 2.5 based on the code provided.
So to retrieve a file that's been uploaded you will need to do something like:
$jFileInput = new JInput($_FILES);
$theFile = $jFileInput->get('jform',array(),'array');
// If there is no uploaded file, we have a problem...
if (!is_array($theFile)) {
JError::raiseWarning('', 'No file was selected.');
return false;
}
// Build the paths for our file to move to the components 'upload' directory
$theFileName = $theFile['name']['tablefile'];
$tmp_src = $theFile['tmp_name']['tablefile'];
$tmp_dest = JPATH_COMPONENT_ADMINISTRATOR . '/uploads/' . $theFileName;
$this->dataFile = $theFileName;
// Move uploaded file
jimport('joomla.filesystem.file');
$uploaded = JFile::upload($tmp_src, $tmp_dest);
// $uploaded contains boolean indicating success or failure
// $tmp_dest will contain final location of file if successful.
Also make sure in your html form you have
enctype="multipart/form-data"
enabled otherwise you won't receive any files in php.
I am trying to get the following function to run when the link is clicked and then load the previous page, but I seem to end up with a blank page with no php errors:
View:
<?php if(is_array($get_images)): ?>
<?php foreach($get_images as $image): ?>
<img src ="<?=base_url()?>includes/uploads/gallery/thumbs/<?=$image['thumbname']?>" alt="<?= $image['description']?>"> Delete
<?php endforeach; ?>
<?php endif; ?>
Controller:
function delete($id) {
$id = $this->uri->segment(3);
$this->image_model->deleteImage($id);
$page['get_images'] = $this->image_model->getImages();
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Delete Gallery Image';
$data['content'] = $this->load->view('admin/deleteimage',$page,TRUE);
}
}
Firstly, a destructive thing such as deleting an image should be POST, not GET.
As for the code, something like this should work (assuming that last closing brace means your function is a class method)...
// However you extract params in your framework.
$id = $request->getParam('id');
// Instantiate the class.
$controller = new ImageController;
// Call the method.
$controller->delete($id);