ERROR – Object of class could not be converted to string
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- kerafura
- Člen | 9
Zdravim, robím v nette jednoduchý kalendár a mám problém že jeho
render je robený cez čisté php skrz class. Problém je že keď mám túto
classu v presenteri, tak to funguje bez chybne, ale nemôžem to napojiť na
databázu, v prípade že to chcem spraviť cez model, aby som dokázal
pripojiť databázu a bolo to prehľadnejšie tak mi to dáva chybu
Object of class could not be converted to string
.
public $calendar;
public function __construct(CalendarModel $calendar)
{
$this->$calendar = $calendar;
}
<?php
namespace App\Models;
use Nette;
class CalendarModel
{
/********************* PROPERTY ********************/
private $database;
private $dayLabels = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
private $currentYear = 0;
private $currentMonth = 0;
private $currentDay = 0;
private $daysInMonth = 0;
private $naviHref = null;
/**
** Constructor
**/
public function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}
/********************* PUBLIC **********************/
/**
** Print out the calendar
**/
public function show() {
$year = null;
$month = null;
if (null == $year && isset($_GET['year'])) {
$year = htmlentities($_GET['year'], ENT_QUOTES);
} elseif (null == $year) {
$year = date("Y", time());
}
if ((!is_numeric($year)) || ($year == "")) {
$year = date("Y", time());
}
if (null == $month && isset($_GET['month'])) {
$month = htmlentities($_GET['month'], ENT_QUOTES);
} elseif (null == $month) {
$month = date("m", time());
}
if ((!is_numeric($month)) || ($month == "")) {
$month = date("m", time());
}
$this->currentYear = $year;
$this->currentMonth = $month;
$this->daysInMonth = $this->_daysInMonth($month, $year);
$content = '<div id="calendar">' . "\r\n" . '<div class="calendar_box">' . "\r\n" . $this->_createNavi() . "\r\n" . '</div>' . "\r\n" . '<div class="calendar_content">' . "\r\n" . '<div class="calendar_label">' . "\r\n" . $this->_createLabels() . '</div>' . "\r\n";
$content .= '<div class="calendar_clear"></div>' . "\r\n";
$content .= '<div class="calendar_dates">' . "\r\n";
$weeksInMonth = $this->_weeksInMonth($month, $year);
// Create weeks in a month
for ($i = 0; $i < $weeksInMonth; $i++) {
// Create days in a week
for ($j = 1; $j <= 7; $j++) {
$content .= $this->_showDay($i * 7 + $j);
}
}
$content .= '</div>' . "\r\n";
$content .= '<div class="calendar_clear"></div>' . "\r\n";
$content .= '</div>' . "\r\n";
$content .= '</div>' . "\r\n";
return $content;
}
/********************* PRIVATE **********************/
/**
** Create the calendar days
**/
private function _showDay($cellNumber) {
$today_day = date("d");
$today_mon = date("m");
$today_yea = date("Y");
$calendarDateDec = $calendarDayNumeric = null;
if ($this->currentDay == 0) {
$firstDayOfTheWeek = date('N', strtotime($this->currentYear . '-' . $this->currentMonth . '-01'));
if (intval($cellNumber) == intval($firstDayOfTheWeek)) {
$this->currentDay = 1;
}
}
if (($this->currentDay != 0) && ($this->currentDay <= $this->daysInMonth)) {
$cellContent = $this->currentDay;
$this->currentDay++;
} else {
$cellContent = null;
}
if ($cellContent != null) {
$calendarDateDec = strtotime($cellContent . '-' . $this->currentMonth . '-' . $this->currentYear);
$calendarDayNumeric = $variable = date('w', $calendarDateDec);
}
if (($calendarDayNumeric == '6' || $calendarDayNumeric == '0') && $cellContent == $today_day && $this->currentMonth == $today_mon && $this->currentYear == $today_yea) {
$class_day = "calendar_today_weekend";
} else if ($cellContent == $today_day && $this->currentMonth == $today_mon && $this->currentYear == $today_yea) {
$class_day = "calendar_today";
} else if ($calendarDayNumeric == '6' || $calendarDayNumeric == '0') {
$class_day = 'calendar_weekend';
} else if ($cellContent == null) {
$class_day = 'calendar_empty';
} else {
$class_day = "calendar_days";
}
$dayOrders = $this->database->table('orders')->where('dateDec', $calendarDateDec);
return '<a href="' . 'order' . '?dayDec=' . $calendarDateDec . '"><div class="' . $class_day . '">' . '<a>' .$cellContent . '</a>' . '</div></a>' . "\r\n";
}
/**
** Create navigation
**/
private function _createNavi() {
$nextMonth = $this->currentMonth == 12 ? 1 : intval($this->currentMonth)+1;
$nextYear = $this->currentMonth == 12 ? intval($this->currentYear)+1 : $this->currentYear;
$preMonth = $this->currentMonth == 1 ? 12 : intval($this->currentMonth)-1;
$preYear = $this->currentMonth == 1 ? intval($this->currentYear)-1 : $this->currentYear;
return '<div class="calendar_header">' . "\r\n" . '<a class="calendar_prev" href="' . $this->naviHref . '?month=' . sprintf('%02d', $preMonth) . '&year=' . $preYear.'">Prev</a>' . "\r\n" . '<span class="calendar_title">' . date('Y M', strtotime($this->currentYear . '-' . $this->currentMonth . '-1')) . '</span>' . "\r\n" . '<a class="calendar_next" href="' . $this->naviHref . '?month=' . sprintf("%02d", $nextMonth) . '&year=' . $nextYear . '">Next</a>' . "\r\n" . '</div>';
}
/**
** Create calendar week labels
**/
private function _createLabels() {
$content = '';
foreach ($this->dayLabels as $index => $label) {
$content .= '<div class="calendar_names"><p>' . $label . '</p></div>' . "\r\n";
}
return $content;
}
/**
** Calculate number of weeks in a particular month
**/
private function _weeksInMonth($month = null, $year = null) {
if (null == ($year)) {
$year = date("Y", time());
}
if (null == ($month)) {
$month = date("m", time());
}
// Find number of days in this month
$daysInMonths = $this->_daysInMonth($month, $year);
$numOfweeks = ($daysInMonths % 7 == 0 ? 0 : 1) + intval($daysInMonths / 7);
$monthEndingDay = date('N',strtotime($year . '-' . $month . '-' . $daysInMonths));
$monthStartDay = date('N',strtotime($year . '-' . $month . '-01'));
if ($monthEndingDay < $monthStartDay) {
$numOfweeks++;
}
return $numOfweeks;
}
/**
** Calculate number of days in a particular month
**/
private function _daysInMonth($month = null, $year = null) {
if (null == ($year)) $year = date("Y",time());
if (null == ($month)) $month = date("m",time());
return date('t', strtotime($year . '-' . $month . '-01'));
}
}
Editoval kerafura (14. 2. 2018 19:57)
- David Matějka
- Moderator | 6445
$this->$calendar = $calendar;
je spatne, ma to byt
$this->calendar = $calendar;
mimochodem, na otazky tykajici se cisteho php koukni spise na https://diskuse.jakpsatweb.cz/