why my localhost server is down symfony 3? - html

i m trying to save data to my base and everything is ok and suddenly when i click at add button in form this screen appears
my output
i havent understand why and where is the problem causing that
in my controller action i have tried to dump my object that i want to add in my database and i got this output so everything is ok object data
this is my controller action
public function addAction(Request $request)
{
if($request->isMethod("post")){
$title = $request->get('title');
$image = $request->get('image');
$genre = $request->get('genre');
$date = $request->get('date');
$director = $request->get('director');
$stars = $request->get('stars');
$trailer = $request->get('trailer');
$synopsis = $request->get('synopsis');
$movie = new Movie();
$movie->setTitle($title);
$movie->setImage($image);
$movie->setGenre($genre);
$movie->setDate($date);
$movie->setDirector($director);
$movie->setStars($stars);
$movie->setTrailer($trailer);
$movie->setSynopsis($synopsis);
dump($movie);
die();
$em= $this->getDoctrine()->getManager();
$em->persist($movie);
$em->flush();
return new Response("film added");
}
return $this->render('FilmsFilmsBundle:Pages:add.html.twig');
}

i have found that my input date is causing this problem
so i changed it by <input type="text"> and i changed the annotation for my date variable in my entity from date to string and everything is working fine

There is obviously a code error there. If you are using apache, you can check the error log on linux using
tail -f /var/log/apache2/error-log.txt
on windows it's usually <base path to apache install>\logs

Related

Codeigniter insert query to database problem

I'm facing an issue to do insert query into the database.
Not sure is a setting issue or a connection issue. The setting I did at .env file while the query I did at controller site
Here is my setting at env file:
CI_ENVIRONMENT = development
app.baseURL = 'http://localhost:8080/'
database.default.hostname = 127.0.0.1 #localhost:8080
database.default.database = learn_ci4_tutorial
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi
here my code at controller site:
public function __construct(){
this->db = \Config\Database::connect();
}
public function insertRaw(){
echo "<h1>Test connection</h1>";
$insert_query = "Insert into tbl_user(name,email,phone_no) values('Alyah','aliyah#gmail.com','123456')";
$this->db->query($insert_query);
if($this->db->query($insert_query)){
echo "<h1>Insert record successful.</h1>";
}else{
echo "<h1>Insert record failed.</h1>";
}
}
here the error come out:
ErrorException #1
Uncaught ErrorException: print_r(): Property access is not allowed yet in C:\xampp\htdocs\ci4\app\Views\errors\html\error_exception.php:96
Which part should I fix so I can do insert query to database?

updating database details in yii2

YII not updating
Tried to GOOGLE but no difference.
public function actionEdit($id)
{
$model = AddMix::findOne($id);
if($model->load(Yii::$app->request->post()) && $model->save()){
Yii::$app->session->setFlash('detailssubmited');
return $this->redirect(['mixes','id'=>$model->id]);
}
return $this->render('editmix',['model'=>$model]);
}
It does not update instead it returns same view with changed value but not in database
It fail the validation so do not enter into the if and re-render the form.
New data are shown because of $model->load(Yii::$app->request->post())
check:
$model->getErrors();
see getErrors documentation

Making PDO Database Connection available in controllers and models

right now, I am kinda frustrated and I hope someone can help me and point me into the right direction.
I have an "old" project which uses the mysql statements for connection to database, etc.
Within this project I have the following:
An index file containing
*
* load configuration and connect to database
*/
$projectConfiguration = new projectConfiguration();
$dbconnect = $projectConfiguration->connect($projectConfiguration->databaseHost, $projectConfiguration->databaseName, $projectConfiguration->databaseUser, $projectConfiguration->databasePass);
// load controller
$ReqMod = FatFramework\Functions::getRequestParameter("mod");
if (!$ReqMod) {
$ReqMod = FatFramework\Functions::getRequestParameter("controller");
}
$module = ($ReqMod) ? $ReqMod : 'default';
In this style I call the views and actions in classes, like SaveAction()
Using mysql always made it very simple to use this database connection in the models called by the controllers like
public function loadCustomersList($sAdditionalWhere = false)
{
$sQuery = "SELECT * FROM customers WHERE 1 ";
if ($sAdditionalWhere) {
$sQuery .= "AND " . $sAdditionalWhere . " ";
}
$sQuery .= "ORDER BY company";
$sql = mysql_query($sQuery);
while (($customer = mysql_fetch_object($sql)) != false) {
$aCustomers[] = $customer;
}
return $aCustomers;
}
I want to totally refractor this project and use PDO. I tried for the last 4 hours to find a solution, but I can't figure out how to make it work.
I think I don't need an extra dbconnect class since PDO is a class itself, am I right?
In the new index file I tried the following:
$db = new database();
try{
$dbc = new PDO($db->get_DbConSettings());
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e)
{
echo 'Verbindung fehlgeschlagen: '.$e->getMessage();
}
But with this $dbc will not available in controllers or models. It there a way to make it available there? If not, what is the best solution?
Do I have to make a database connection in every model?
An other issue I have with this is:
$db->get_DbConSettings()
in
$dbc = new PDO
gives back
'mysql:host=127.0.0.1; dbname=c1virtbkk', 'root', '123'
($dbc = new PDO('mysql:host=127.0.0.1; dbname=c1virtbkk', 'root', '123');)
I cannot connect to the database. I get the following:
Verbindung fehlgeschlagen: could not find driver
If I don't use $db->get_DbConSettings and put the required information manually in, I don't get any error and can do queries. Any hints?
Help is really appreciated.
Thanks in advance!
Mark
Definitely don't create a new PDO connection in each model. Creating MySQL database connections is fairly quick, but there's still some overhead to doing so. You want to reuse a connection throughout your request. If nothing else, it allows you to share a transaction across multiple models.
Some frameworks store shared resource objects in a "registry" class which is a singleton key-value store. It's not really much more than a global hash array, but making it a class makes the registry itself more easily tested with PHPUnit. See https://framework.zend.com/manual/1.12/en/zend.registry.using.html for an example of a registry.
You're right that PDO is a class, even though it's implemented as a C extension instead of a PHP class. But it's a class, and new PDO(...) returns an object of that class.
One reason to create a db class of your own is to help you in unit-testing, because you could create a mock object for your db class so you can test your other classes (even model classes) without needing a live database connection. Your db class could extend or else contain a PDO object.
Your issue about the error "could not find driver" is probably because the PDO driver for mysql is not installed. PDO is one PHP extension, and then there's a separate extension for each brand of SQL database. You can confirm this with:
$ php -i
...lots of output...
PDO
PDO support => enabled
PDO drivers => mysql, odbc, sqlite
pdo_mysql
PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $
...more output for other extensions...
Note that PDO tells me which drivers I have installed: mysql, odbc, and sqlite.
So you need to install pdo_mysql. I'm not sure what OS you're on, but I'm often on CentOS Linux or Ubuntu Linux. The pdo_mysql is available as a separate package via yum or apt.
Re your comment:
Okay, here's an example of a registry:
class registry {
protected static $items = array();
public static get($key) {
return isset(self::$items[$key])?
self::$items[$key] : null;
}
public static set($key, $object) {
self::$items[$key] = $object;
}
}
In your controller initial code, you'd create a database object and store it in the registry:
$projectConfiguration = new projectConfiguration();
$dbconnect = $projectConfiguration->connect(
$projectConfiguration->databaseHost,
$projectConfiguration->databaseName,
$projectConfiguration->databaseUser,
$projectConfiguration->databasePass);
registry::set('db', $dbconnect);
Then in your model class methods (or anywhere you need the database), get the db object from the registry and use it:
public function loadCustomersList($sAdditionalWhere = false)
{
$sQuery = "SELECT * FROM customers WHERE 1 ";
if ($sAdditionalWhere) {
$sQuery .= "AND " . $sAdditionalWhere . " ";
}
$sQuery .= "ORDER BY company";
$db = registry::get('db');
$stmt = $db->query($sQuery);
$aCustomers = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $aCustomers;
}

Write database queries in Magento custom module

I created a custom module in my Magento. This is working good. My module's name is mymodule.
Mymodule.php
class Myshop_Mymodule_Block_Mymodule extends Mage_Core_Block_Template
{
public function myfunction()
{
return "Hello User";
}
}
The path of the Mymodule.php file is C:\wamp\www\magento\app\code\local\Myshop\Mymodule\Block.
Now I want to display some data from database. For example I would like to display the admin's email id. How can I display this??
I tried like this.
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
//database write adapter
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $read->fetchAll("select email from admin_user where user_id= 1");
var_dump($result);
I write these lines in Mymodule.php inside myfunction. But nothing displayed(display only "Hello User").
So my question is how to display or write database queries in magento custom module.
Please someone help me..Any help is really appreciable..
We generally avoid using adapters when magento by default provides models for the basic tables. In your case, you can fetch the admin details using following :
<?php
$userDetails = Mage::getModel('admin/user')->load(1);
//where 1 is your admin user id
echo $userDetails->getEmail();
?>
Hence, your function can be modified as :
<?php
class Myshop_Mymodule_Block_Mymodule extends Mage_Core_Block_Template
{
public function myfunction()
{
$userDetails = Mage::getModel('admin/user')->load(1);
return $userDetails->getEmail();
}
}
You can get data from database in magento as
$collection = Mage::getModel("mumodule/mymodule")->getCollection();
foreach($collection as $data){
..Your Code ..
}

Codeigniter timezone mysql settings

Just realised WHY my site is now showing all datetime variables as -1 hr... I'm using Codeigniter for the first time! (Never had this problem before)
So, I have included the following code in my main index.php file
/*
|---------------------------------------------------------------
| DEFAULT TIMEZONE
|---------------------------------------------------------------
|
| Set the default timezone for date/time functions to use if
| none is set on the server.
|
*/
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('GMT');
}
However, it's still showing as -1 hr, so I'm assuming I need to set some sort of default setting for MySQL...
I have included the following line of code in my model:
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db->query("SET time_zone='+0:00'");
}
Still no difference... Help!
My code is:
<h3><?=date('D, jS F # g:ia', strtotime($row->datetime))?></h3>
The $row->datetime variable is nothing more than a DATETIME column value from my MySQL database. The echoed variable in view is ALWAYS 1 hour less than the value in my database...
My model code is:
function coming_up()
{
$this->db->query("SET time_zone='+0:00'");
$query = $this->db->query('SELECT * FROM events1 WHERE datetime >= NOW() ORDER BY datetime LIMIT 2');
return $query->result();
}
In config/autoload.php, set a model to load on each page load. then call $this->db->query("SET time_zone='+0:00'"); in that model constructor.
config/autoload.php
$autoload['model'] = array('default_model');// for ex, "say default_model"
In application/models, create a new model file with name of "default_model.php" and add below code.
application/models/default_model.php
class Default_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db->query("SET time_zone='+0:00'");
}
}
On each page load, this constructor will be called and mysql timezone will be set to +0:00.
Add these line in your config file and then check, it is working for me
$config['time_reference'] = 'gmt';# Default should be GMT
date_default_timezone_set('UTC');# Add this line after creating timezone to GMT for reflecting
am also face this problem.
i tried this way..
$this->db->query("SET LOCAL time_zone='Asia/Kolkata'");
$query="SELECT * FROM offers where NOW() between offfer_from and offer_to";
$res=$this->db->query($query);
it will work fine in my project and i'm not using anything new default model.. if you want global solution means you can use auto-load some model.
SET default time_zone for MySQL :
Go to : Your_Project/system/core/Model.php And then update this function :
public function __construct() {
$this->db->query("SET time_zone='YOUR_TIME_ZONE'");
}
SET default_time_zone for PHP :
Go to Your_Project/index.php
And then update here :
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
date_default_timezone_set('YOUR_TIME_ZONE');
I like the solution proposed by Kumar however, I didn't need to set this globally, only when showing dates stored in UTC time in my db.
In my api model I have the following function.
public function setDBTimeOffset(){
$dt = new DateTime();
$offset = $dt->format("P");
$result = $this->db->query("SET time_zone='$offset';");
return $result;
} # END FUNCTION setDBTimeOffset
From my controller, I call the following first.
$this->api->setDBTimeOffset();
Followed by the call to the function that queries the db. For example, I am fetching some user history.
$data["viewData"]["allHistory"] = $this->api->getUserHistory("ALL", 0, 10000);
When I display the output, the dates are in my timezone. I hope this helps someone, somewhere in time (couldn't resist).
You can set it in the index.php file in your project folder on top and after <?php
<?php
date_default_timezone_set('Asia/Bangkok');