Joomla.it Forum

Non solo Joomla... => Sviluppo => : memma1989 20 May 2016, 14:56:53

: Problema inserimento data in database MySql
: memma1989 20 May 2016, 14:56:53
Buongiorno a tutti fedelissimi di Joomla,
sto sviluppando una componente e mi è sopraggiunto un problema.


Sto cercando di inserire una data in una tabella in un database MySql ma non riesco in nessun modo.
Se cerco di inserire questa data viene inserito il valore di default 0000-00-00 perchè è come se non avesse un formato giusto.
La mia tabella in cui sto cercando di inserire la data è la seguente:


sensors(code, nodeId, quantityId, installationDate);


La colonna che mi da problemi è proprio installationDate che è di tipo date.
Il codice del mio model di Joomla SensorsManagerModelAddedSensor è il seguente:


:
/* Retrieving parameters from POST */
$jinput = JFactory::getApplication()->input;
$selectedidnode = $jinput->getInt('selectnode');
$selectedquantity = $jinput->get('selectquantity');
$selectedcode = $jinput->getInt('selectcode');
$selecteddate = $jinput->get('selectdate');


/* This conversion has no effect */$converteddate = JFactory::getDate($selecteddate)->format('Y-m-d');
$converteddateanother = $converteddate->toMysql();
$converteddate = new JDate($selecteddate);
$converteddateanother = $converteddate->format('Y-m-d')->toMysql();*/

/* Retrieving quantity id from Db */
$query->select($db->quoteName('id'));
$query->from($db->quoteName('fos_quantity'));
$query->where($db->quoteName('name') . ' LIKE '. $db->quote($selectedquantity));

$db->setQuery($query);
$quantityId = $db->loadResult();


Qualche idea?
Ringrazio tutti anticipatamente


Giulia[/code]
: Re:Problema inserimento data in database MySql
: MariaElenaBoschi 20 May 2016, 16:36:53
io vedo una select non un insert
: Re:Problema inserimento data in database MySql
: memma1989 20 May 2016, 16:41:55
Chiedo scusa, ho dimenticato un pezzo di codice. Grazie per avermelo fatto notare! :)


:


<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_sensorsmanager
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
 
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
 * AddedSensor Model
 *
 * @since  0.0.1
 */
class SensorsManagerModelAddedSensor extends JModelItem{
    
/* Performing three queries:
    1. Retrieve quantityId from Db
    2. Insert sensor from Db
    3. Retrieve all sensors
    */
    
public function getAddedSensor(){
        
$db JFactory::getDbo();
        
$query $db->getQuery(true);

        
/* Retrieving parameters from POST */
        
$jinput JFactory::getApplication()->input;
        
$selectedidnode $jinput->getInt('selectnode');
        
$selectedquantity $jinput->get('selectquantity');
        
$selectedcode $jinput->getInt('selectcode');
        
$selecteddate $jinput->get('selectdate');

        
/*$converteddate = JFactory::getDate($selecteddate)->format('Y-m-d');
        $converteddateanother = $converteddate->toMysql();
        $converteddate = new JDate($selecteddate);
        $converteddateanother = $converteddate->format('Y-m-d')->toMysql();*/

        /* Retrieving quantity id from Db */
        
$query->select($db->quoteName('id'));
        
$query->from($db->quoteName('fos_quantity'));
        
$query->where($db->quoteName('name') . ' LIKE '$db->quote($selectedquantity));

        
$db->setQuery($query);
        
$quantityId $db->loadResult();

        
$convertedquantityid = (int)$quantityId;

        
/* Adding sensor into Db */
        
$columns = array('code''nodeId''quantityId''installationdate');
        
$values = array($selectedcode$selectedidnode$convertedquantityid$selecteddate);

        
$query2 $db->getQuery(true);
        
$query2->insert($db->quoteName('fos_sensor'))
            ->
columns($db->quoteName($columns))
            ->
values(implode(','$values));

        
$db->setQuery($query2);
        
$db->execute();
}


: Re:Problema inserimento data in database MySql
: MariaElenaBoschi 20 May 2016, 16:53:50
$selecteddate = $jinput->get('selectdate');
---------------------------------------------------------------
se dopo qs metti

print_r($selecteddate); die();

cosa vedi?

probabilmente non è nel formato corretto

poi usa $date->toSql()

: Re:Problema inserimento data in database MySql
: memma1989 20 May 2016, 17:07:36
Vedo 2016-05-07

Il Debugger sembra mi segnali che si tratti di una normalissima stringa.
Ho provato ad usare la funzione toSql() ma niente mi dice

Fatal error: Call to a member function toSql() on a non-object in C:\Users\Giulia\Desktop\xampp\htdocs\fostirocinio\components\com_sensorsmanager\models\addedsensor.php on line 34

 :-[
: Re:Problema inserimento data in database MySql
: memma1989 20 May 2016, 17:15:59
Mi spiego meglio: se aggiungo le due righe di codice


:


$converteddate = JFactory::getDate($selecteddate)->format('Y-m-d');
$converteddateanother = $converteddate->toSql();




allora ottengo l'errore Fatal error: Call to a member function toSql() on a non-object in C:\Users\Giulia\Desktop\xampp\htdocs\fostirocinio\components\com_sensorsmanager\models\addedsensor.php on line [/size]35
: Re:Problema inserimento data in database MySql
: MariaElenaBoschi 20 May 2016, 17:59:34
le funzioni di JDate sono tutti fork di php date() comunque...

$date = new JDate($selecteddate);

$tuadata = $date->toSql();

Anche per l'update è è più semplice usare la struttura oggetto
https://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
:
Using an Object

The JDatabaseDriver class also provides a convenient method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.

// Create and populate an object.
$profile = new stdClass();
$profile->user_id = 1001;
$profile->profile_key='custom.message';
$profile->profile_value='Inserting a record using insertObject()';
$profile->ordering=1;
 
// Insert the object into the user profile table.
$result = JFactory::getDbo()->insertObject('#__user_profiles', $profile);
: Re:Problema inserimento data in database MySql
: memma1989 21 May 2016, 11:27:15
Grazie mille per la tua cortesia e attenzione!


Appena lo provo ti faccio sapere.


Grazie grazie grazie!  ;D
: Re:Problema inserimento data in database MySql
: memma1989 25 May 2016, 11:19:51
In nessun modo è stato possibile effettuare la conversione che mi hai suggerito


:
$date = new JDate($selecteddate);$tuadata = $date->toSql();


Avevo già provato in tutte le salse e non sono riuscita a farlo funzionare.
In compenso il tuo suggerimento si usare un oggetto per fare l'inserimento ha funzionato, adesso la data la salva come si deve nel db.


Grazie mille!!!  :D