Undefined index error upon $em->clear() in Symfony2 - php

I have written a Symfony command to import some data from an API. It works but the problem is my PHP memory usage increases when I insert a big JSON in my database. And my unitOfWork increases by '2' to after each activty import.
I have already unset all my used objects, and I have read the documentation of Symfony2 when you want to do massive batch: http://www.doctrine-project.org/blog/doctrine2-batch-processing.html
But when I use $em->clear() my entity manager gives this error:
Notice: Undefined index: 000000007b56ea7100000000e366c259 in path-to-application\vendor\doctrine\lib\Doctrine\ORM\UnitOfWork.php line 2228
Here is my complete code :
/**
* #see Command
*/
protected function configure() {
$this
->setName('ks:user:runkeepersync')
->setDescription('Synchroniser les activités d\'un utilisateur runkeeper')
->setDefinition(array(
new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'),
))
}
/**
* #see Command
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$accessToken = $input->getArgument('access_token');
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken);
if (!is_object($UserHasServices) ) {
echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken."";
}
$user = $UserHasServices->getUser();
$service = $UserHasServices->getService();
echo "avant de requérir l'api : ".memory_get_usage()."\n";
try {
$rkApi = $this->getContainer()->get('ks_user.runkeeper');
$rkApi->setAccessToken($accessToken);
$activities = $rkApi->getFitnessActivities(0,25);
$nbParPages = 25;
$nomberActivitites = $activities->size;
$aActivities = $activities->items;
$nbPages = floor ($nomberActivitites/$nbParPages);
$aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other");
$aEnduranceUnderWater = array("Swimming");
$enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance");
if (!is_object($enduranceOnEarthType) ) {
echo "Impossible de trouver le type de sport d'endurance";
}
$enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water");
if (!is_object($enduranceUnderWaterType) ) {
echo "Impossible de trouver le type de sport d'endurance sous l'eau ";
}
echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n";
$a = 0;
for($i=0;$i<=$nbPages;$i++){
if($i!=0){
$activities = $rkApi->getFitnessActivities($i,25);
$aActivities = $activities->items;
}
foreach ($aActivities as $activity) {
$a = $a+1;
$codeSport = $this->formatNameSport($activity->type);
$sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport);
if (!is_object($sport) ) {
$sport = new \Ks\ActivityBundle\Entity\Sport();
$sport->setLabel($codeSport);
$sport->setCodeSport($codeSport);
$sport->setSportType($enduranceOnEarthType);
$em->persist($sport);
$em->flush();
}
$activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri));
if(in_array($activity->type, $aEndurance)){
$urlActivitieDetail = $activityDetail->activity;
$ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user);
isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : "";
isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : "";
isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : "";
$ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now'));
$ActivitySessionEnduranceOnEarth->setSport($sport);
isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : "";
isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : "";
$maxElevation = 0;
$minElevation = 10000;
if(isset($activityDetail->path)){
foreach($activityDetail->path as $gpsPoint){
if($gpsPoint->altitude > $maxElevation){
$maxElevation = $gpsPoint->altitude;
}
if($gpsPoint->altitude < $minElevation){
$minElevation = $gpsPoint->altitude;
}
}
$ActivitySessionEnduranceOnEarth->setElevationMin($minElevation);
$ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation);
}
$em->persist($ActivitySessionEnduranceOnEarth);
$em->flush();
//Pour chaque activité on a un identifiant relatif au service qu'on synchronise
$ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService();
$ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth);
$ActivityComeFromService->setService($service);
$ActivityComeFromService->setIdWebsiteActivityService($activity->uri);
$ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri));
$ActivityComeFromService->setTypeSource("JSON");
$em->persist($ActivityComeFromService);
$em->flush();
echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n";
unset($ActivitySessionEnduranceOnEarth);
unset($ActivityComeFromService);
echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n";
}
if(in_array($activity->type, $aEnduranceUnderWater)){
$ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user);
isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : "";
isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : "";
isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : "";
$ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now'));
$ActivitySessionEnduranceUnderWater->setSport($sport);
isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : "";
isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : "";
$em->persist($ActivitySessionEnduranceUnderWater);
$em->flush();
$ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService();
$ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater);
$ActivityComeFromService->setService($service);
$ActivityComeFromService->setIdWebsiteActivityService($activity->uri);
$ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri));
$ActivityComeFromService->setTypeSource("JSON");
$em->persist($ActivityComeFromService);
$em->flush();
echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n";
unset($ActivitySessionEnduranceUnderWater);
unset($ActivityComeFromService);
}
echo "Après chaque activité : ".memory_get_usage()."\n";
unset($sport);
unset($activityDetail);
$em->clear();
}
}
} catch (\Exception $e) {
throw $e;
}
}
Thanks, #AdrienBrault. I have tested with --env=prod --no-debug, and it is true that it consumes less memory, but the memory still increase. How can I really clear the entity manager? and stabilize the memory?

Symfony logs all SQL queries in dev environment, so first you need to disable it
// disable logger
$em->getConnection()->getConfiguration()->setSQLLogger(null);
You may use event listeners on entities, it may also increase memory usage. You can disable them like so
// remove all listeners
foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
foreach ($listeners as $listener) {
$em->getEventManager()->removeEventListener($event, $listener);
}
}
Remove unset from your code, there is no need for them, as you clear entity manager every step of your loop.
// save and clear
$em->flush();
$em->getUnitOfWork()->clear();
Remember that doctrine can optimize your queries, and improve perfomance if you group queries into one flush. So the best practice would be to execute flush once over some parts of your data. For example:
// collect 100 entities and then save them
if (($i % 100) == 0) {
$em->flush();
$em->getUnitOfWork()->clear();
}

Try to reset the entity manager with:
$this->getContainer()->get('doctrine')->resetEntityManager();
and then:
$em = $this->getContainer()->get('doctrine')->getEntityManager();

Related

Problem with orders and product orders laravel

Good evening, I have an orders and orders_products table:
A supplier (user) can upload products to sell them.
If buyer buys a product from different suppliers. That he has for example 3 products buy from different supplier, I want to separate.
For each product purchased from different suppliers it will represent an order for the buyer made by the buyer.
I made my code for the order but I can't seem to place an order for each supplier, please I really need help.
This is my PHP code:
public function validation_commande(Request $request){
$utilisateur = Utilisateur::where('id','=',session('utilisateur'))->first();
$paniers = Panier::where('utilisateur_id','=',$utilisateur->id)->get();
$utilisateur_id = $utilisateur->id;
$mode_paiement_id = 4;
$adresse = Adresse::where('utilisateur_id','=',$utilisateur_id)
->where('adresse_par_defaut','=',1)
->first();
$total_ht = 0;
$total_ttc = 0;
$nombre_produit_panier = count($paniers);
if($nombre_produit_panier >= 1)
{
foreach ($paniers as $c) {
$total_ht += $c->produit->prix_total($c->quantite);
$total_ttc += $c->produit->prix_total_ttc($c->quantite);
$commande = Commande::firstOrCreate([
'utilisateur_id'=>$utilisateur_id,
'fournisseur_id'=>$c->produit->utilisateur_id,
'boutique_id'=>$c->produit->boutique_id,
'statut'=>'en_cours_de_traitement',
'statut_fournisseur'=>'pas_encore_vue_sa_commande',
'total_ht'=>$total_ht,
'total_ttc'=>$total_ttc,
'mode_paiement_id'=>$mode_paiement_id,
'adresse_id'=>$adresse->id,
'date_commande'=>now()
]);
break; }
if($c->achat_groupe == 1){
$achat_groupe = 1;
}else{
$achat_groupe = 0;
}
foreach ($paniers as $c) {
CommandeProduit::create([
'commande_id'=>$commande->id,
'produit_id'=>$c->produit->id,
'prix_ht'=>$c->produit->prix_sans_taxe($c->quantite),
'prix_ttc'=>$c->produit->prix($c->quantite),
'quantite'=>$c->quantite,
'utilisateur_id'=>$utilisateur->id,
'fournisseur_id'=>$c->produit->utilisateur_id,
'boutique_id'=>$c->produit->boutique_id,
'achat_groupe'=>$achat_groupe,
'date_creation'=>now()
]);
}
$message_a_envoye = "**** Market Makers **** \n \n";
$message_a_envoye .= "Une nouvelle commande vient d'arriver";
// $date_du_jour = date("d-m-Y H:i:s");
// $message_a_envoye .= "Date d'inscription : ".$utilisateur->date_creation."\n";
// $this->telegram_send(urlencode($message_a_envoye));
// sleep(3);
$this->telegram_send_2(urlencode($message_a_envoye));
$lien_button = env('APP_URL').'/mes-commandes-b/';
$data = [
'subject' => "Nouvelle commande",
'titre_page' => 'Vous avez reçu une nouvelle commande',
'message' => 'Vous avez reçu une nouvelle commande sur votre espace Market Makers',
'lien_button' => $lien_button,
'message_button' => 'Voir'
];
// Mail::to($c->produit->utilisateur->email)->send(new NotificationMail($data,$c->produit->utilisateur));
Session::flash('success','Votre commande a bien été effectué avec succès');
Panier::where('utilisateur_id', $utilisateur->id)->delete();
$request->session()->put('venue_du_panier',null);
return redirect(route('mes-commandes'));
}
else
{
return redirect(route('mes-commandes'));
}
}
http://pastebin.fr/96873

Ensure no missing column value is sent to INSERT statement

I recently changed my website from shared hosting to a dedicated server (major upgrade..), and some things didn't work out of the box. One of these things was the INSERTs. I was used to code INSERTS without bothering about having all the columns values in my statement. The missing columns were automatically filled with 0s and threw basic warning I ignored.
I thought it was normal MySQL\MariaDB behaviour. With my new dedicated server (not hosted locally), the database sends an error and refuses to insert the line if all the columns values are not declared in the INSERT statement. This is a problem. I add columns, from time to time, in many tables, as I improve the site's functionalities. I don't want to be revising all the INSERTS in the thousands of lines in my PHP scripts every time I add a column to a table.
So, instead, I created this insert function.
Basically:
Call the function, send an array with table name and key+values I want to insert
Function reads the table name, selects one line, and list the columns name
Function prepares an INSERT with the passed key+values, and generates bulk (depending on the types) values for the columns without values passed
Proceed to INSERT
Now the questions:
Is there a simpler way of doing this?
Am I breaching something?
Is it a recurrent need?
And here the function code:
// INSERTION DB GÉNÉRALE
function insert_db($valeurs)
{
global $date;
global $no;
global $bdd;
// Pour le log
$log = 'Valeurs reçues BDD : ';
foreach ($valeurs as $colonne => $valeur)
{
$log .= $colonne.' = '.$valeur.' || ';
};
// On obtient la table dans $valeurs['db_table']
$resultat = $bdd->query('SELECT * FROM '.$valeurs['db_table'].' LIMIT 1');
$tous_les_champs = array_keys($resultat->fetch(PDO::FETCH_ASSOC)); // On crée un tableau avec toutes les entêtes de colonnes
$champs = array(); // Les champs qui seront mis au début de la requête
$valeurs_preparees = array(); // Les valeurs préparées (avec un : devant) pour mettre dans la deuxième portion de la requête
foreach ($tous_les_champs as $champ_unique) // On valide qu'on a des données pour chaque colonne
{
$valeur = "0";
if (strpos($champ_unique, 'DATE') !== false) { $valeur = $date; }; // Si le titre du champs contiens « DATE », on prends en considération qu'il faut y mettre quelque chose en type datetime
if (strpos($champ_unique, 'RAW_DN') !== false) { $valeur = "0000-00-00 00:00:00"; }; // Pour le champs custom de date de naissance de USAGERS
if (strpos($champ_unique, 'DATE_DEPART') !== false) { $valeur = "0000-00-00 00:00:00"; }; // Si le titre du champs contiens « DATE », on prends en considération qu'il faut y mettre quelque chose en type datetime
if (strpos($champ_unique, 'DESCRIPTION') !== false) { $valeur = "Aucune description"; };
if (strpos($champ_unique, 'COMMENTAIRE') !== false) { $valeur = "Aucun commentaire"; };
if (strpos($champ_unique, 'CREE_PAR') !== false) { $valeur = $_SESSION['DOCTUS'][$no]['ID']; };
if (strpos($champ_unique, 'MODIF_PAR') !== false) { $valeur = $_SESSION['DOCTUS'][$no]['ID']; };
if (strpos($champ_unique, 'CREATEUR') !== false) { $valeur = $_SESSION['DOCTUS'][$no]['ID']; };
if (strpos($champ_unique, 'MODIFICATEUR') !== false) { $valeur = $_SESSION['DOCTUS'][$no]['ID']; };
if (strpos($champ_unique, 'PAYE_') !== false) { $valeur = "1"; };
if (!isset($valeurs[$champ_unique]) || $valeurs[$champ_unique] == "") { $tableau[$champ_unique] = $valeur; }else{ $tableau[$champ_unique] = $valeurs[$champ_unique]; }; $champs[$champ_unique] = $champ_unique; $valeurs_preparees[$champ_unique] = ":".$champ_unique;
};
// On crée les champs pour la requête
$requete_champs = "";
foreach ($champs as $champ_unique)
{
if ($requete_champs != "") { $requete_champs .= ', '; };
$requete_champs .= $champ_unique;
};
// On crée les valeurs pour la requête
$requete_valeurs = "";
foreach ($valeurs_preparees as $valeur_unique)
{
if ($requete_valeurs != "") { $requete_valeurs .= ', '; };
$requete_valeurs .= $valeur_unique;
};
$req_insert_db = $bdd->prepare('INSERT INTO '.$valeurs['db_table'].'('.$requete_champs.') VALUES('.$requete_valeurs.')');
$req_insert_db->execute($tableau);
$id_insert = $bdd->lastInsertId();
// On log l'insertion
$log .= "\n ID inséré : ".$id_insert;
if ($id_insert == 0) { $log .= ' || ERREUR'; };
$fichier = './log/insertions_log.txt';
file_put_contents($fichier, $log, FILE_APPEND);
return $id_insert;
};
The cause is a different configuration of MySql/MariaDb server between old and new server
(see the documentation of Strict SQL Mode for more details).
The cleanest way is to do an ALTER TABLE of all your tables and give to any fields a DEFAULT value (at least to all fields that you don't want to specify a value): in this case the engine don't need all fields in INSERT statements. (see this answer to see the syntax)

Add an array within an array list Codeigniter 3?

Hello community have a query, the issue is that I have a method that consulted the database, which invokes the method assume time parametrically.
As I commented'm using the PHP framework CodeIgniter, the question is this once consulted and loaded the first list of arrays, called: $listSubPrim
I want that list of arrays, add another array that is in the list $listSubSecu, but the issue is that I notice that does not work the way I want, although the method add array_push
principal_model.php
<?php
class Principal_model extends CI_Model {
public function __construct() {
$this->load->database();
}
function obtenerPermisosNivel($icodrol, $nivelmenu) {
try{
$sql = 'SELECT ICODMAEMENU, ICODROL, VDESMAEMENU, VDESICONO, VDESIDMAEMENU, ';
$sql = $sql.'ICODPADMENU, VDESCOMAND, SIORDPRIORIDAD, ICODSUBMENU, BACTIVO ';
$sql = $sql.'FROM TABMAEMENU ';
$sql = $sql.'WHERE ICODROL = ? ';
$sql = $sql.'AND BACTIVO = ? ';
switch ($nivelmenu) {
case NIVEL_SUB_MENU_PRIMARIO:
$sql = $sql.'AND ICODPADMENU IS NULL ';
$sql = $sql.'ORDER BY ICODMAEMENU ';
break;
case NIVEL_SUB_MENU_SECUNDARIO:
$sql = $sql.'AND ICODPADMENU IS NOT NULL ';
$sql = $sql.'AND ICODSUBMENU IS NULL ';
$sql = $sql.'ORDER BY SIORDPRIORIDAD ';
break;
case NIVEL_SUB_MENU_TERCIARIO:
$sql = $sql.'AND ICODPADMENU IS NOT NULL ';
$sql = $sql.'AND ICODSUBMENU IS NOT NULL ';
$sql = $sql.'ORDER BY SIORDPRIORIDAD ';
break;
}
$query = $this->db->query($sql, array($icodrol, ESTADO_ACTIVO));
return $query->result_array();
} catch(Exception $e){
log_message('debug', $e->getMessage()); // use codeigniters built in logging library
show_error($e->getMessage()); // or echo $e->getMessage()
}
}
function obtenerPermisosMenu($icodrol) {
try{
/* Obtenemos el listado de SubMenus Primarios de toda la lista */
$listSubPrim = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_PRIMARIO);
/* Obtenemos el listado de SubMenus Secundarios de toda la lista */
$listSubSecu = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_SECUNDARIO);
/* Obtenemos el listado de SubMenu de asociado al SubMenu primario */
foreach ($listSubPrim as $pri) {
$listSubMenuItem = array();
foreach ($listSubSecu as $sec) {
if($sec['ICODPADMENU'] == $pri['ICODMAEMENU']) {
array_push($listSubMenuItem, $sec);
}
}
if (count($listSubMenuItem) > 0) {
array_push($pri, $listSubMenuItem);
}
}
/* Obtenemos el listado de SubMenus Terciarios de toda la lista */
$listSubTerc = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_TERCIARIO);
/* Obtenemos el listado de SubMenu de asociado al SubMenu secundario */
foreach ($listSubPrim as $pri) {
$listSubSecu = $pri[10];
if (is_array(listSubSecu)) {
foreach (listSubSecu as $sec) {
$listSubMenuItem = array();
foreach ($listSubTerc as $ter) {
if($sec['ICODMAEMENU'] == $ter['ICODSUBMENU']) {
array_push($listSubMenuItem, $sec);
}
}
array_push($sec, $listSubMenuItem);
}
}
}
return $listSubPrim;
} catch(Exception $e){
log_message('debug', $e->getMessage()); // use codeigniters built in logging library
show_error($e->getMessage()); // or echo $e->getMessage()
}
}
}
?>
I realize that walking back on the list: $listSubPrim
Limited position 10 of the array should be an array, so as indicated in the code.
$listSubSecu = $pri[10];
I hope you have understood my question.
Basically I want just a list of fixes, with three levels.
Thank you.
Hello I will explain more clearly the impression the first list of arrays: $listSubPrim
$listSubPrim = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_PRIMARIO);
$listSubSecu = $this->obtenerPermisosNivel($icodrol, NIVEL_SUB_MENU_SECUNDARIO);
foreach ($listSubPrim as $pri) {
log_message('debug', '-> '.$pri['ICODMAEMENU'].' - '.$pri['ICODROL'].' - '.$pri['VDESMAEMENU']);
}
I printed the results of a list of arrays, as you will notice:
DEBUG - 2015-06-10 16:43:31 --> -> 85 - 2 - Las 20 Mejores Ofertas
DEBUG - 2015-06-10 16:43:31 --> -> 86 - 2 - Ofertas Inteligentes
DEBUG - 2015-06-10 16:43:31 --> -> 87 - 2 - Descuentos Restaurantes
DEBUG - 2015-06-10 16:43:31 --> -> 88 - 2 - Categorias
I need you in that row, add the list of array: $listSubSecu, for each row that is within the array foreach.
Aya hope I understood.
Thank you.

PHP - Add a entrance into a Mysql table using OOP

i want to add an entrance (using a form) into a Mysql table using POO.
Here is what i did :
My class :
<?php
class portfolio{
public $title;
public $img_desk;
public $img_tablet;
public $img_phone;
public $id;
public function __construct($title, $img_desk, $img_tablet, $img_phone, $port_id){
$this->title = $title;
$this->img_desk = $img_desk;
$this->img_tablet = $img_tablet;
$this->img_phone = $img_phone;
$this->id = $port_id;
}
public function getPortfolio($id){
$reponse = $bdd->query('SELECT * FROM designer WHERE id = $id');
$portfolio = $reponse->fetch();
$a = new portfolio($portfolio['title'], $portfolio['img_desk'], $portfolio['img_tablet'], $portfolio['phone'], $portfolio['id']);
return $a;
}
public function addPortfolio($title, $img_desk, $img_tablet, $img_phone){
$add = $bdd->exec('INSERT INTO designer(title, img_desk, img_tablet, img_phone) VALUES(:title, :img_desk, :img_tablet, :img_phone)');
$add->execute(array( // _> was used.!!!
'title' => $title,
'img_desk' => $img_desk['name'],
'img_tablet' => $img_tablet['name'],
'img_phone' => $img_phone['name']
// imgs...
));
if (isset($img_desk) AND $img_desk['error'] == 0){
// Testons si le fichier n'est pas trop gros
if ($img_desk['size'] <= 3000000)
{
require("class/img.class.php");
// Testons si l'extension est autorisée
$infosfichier = pathinfo($img_desk['name']);
$extension_upload = $infosfichier['extension'];
$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
if (in_array($extension_upload, $extensions_autorisees))
{
// On peut valider le fichier et le stocker définitivement
move_uploaded_file($img_desk['tmp_name'], 'img/port/' . basename($img_desk['name']));
}
}
}
echo 'Élement ajouté';
}
public function editPortfolio($old, $new){
$edit = $bdd->prepare('UPDATE designer SET title = :title, img_desk = :img_desk, img_tablet = :img_tablet, img_phone = :img_phone WHERE id = $old->id ');
$edit->execute(array(
'title' => $new->title,
'img_desk' => $new->img_desk,
'img_tablet' => $new->img_tablet,
'img_phone' => $new->img_phone
));
}
public function deletePortfolio($cible){
//DELETE FROM designer WHERE id = $cible->id;
}
public function getAllPortfolio(){
//$reponse = $bdd->query('SELECT * FROM designer');
//$donnees = $reponse->fetch();
//return $donnees;
}
}
?>
And here is my using on a php file containing the form :
<?php
include('class/portfolio.class.php');
if(isset($_POST['title'])){
addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
echo 'ok';
}else{
?>
<form method="post" action="admin.php" enctype="multipart/form-data">
<input type="text" placeholder="Titre" name="title"><br>
<input type="file" name="img_desk"><br>
<input type="file" name="img_tablet"><br>
<input type="file" name="img_phone"><br>
<input type="submit" value="Ajouter" name="ok">
</form>
<?php
}
?>
And it's telling me :
Fatal error: Call to undefined function addPortfolio() in C:\wamp\www\designers\admin.php on line 47
Would someone explain me please ? I know i'm doing things wrong but i'm actually learning OOP.
Thank you in advance : ).
You need to create an object first of the class portfolio
So, the corrected code should be:
// YOUR CODE HERE.
$portfolio = new portfolio;
if(isset($_POST['title'])){
$portfolio->addPortfolio($_POST['title'], $_FILES['img_desk'], $_FILES['img_tablet'], $_FILES['phone']);
echo 'ok';
}else{
// YOUR CODE HERE
Or you can directly use the function like this
provided the function is declared Static but since you have personal data so not recommended!
portfolio::addPortfolio();

PHP. Calling function from a different class.

I am working with two classes: usuarios, preguntas.
In preguntas I store id_usuario which correspond to the id from user, ok. But sometimes I need to display more than the id, so I made a function in usuarios. php to print this info:
This is mi code for now
usuarios.php (I'm only including relevant code for this problem)
Código PHP:
function __construct($id){
$consulta = mysql_query("SELECT * FROM usuarios WHERE id = '".$id."'");
while($item = mysql_fetch_array($consulta)){
$this->id = $item['id'];
$this ->fid = $item['fid'];
$this ->usuario = $item['alias'];
$this ->password = $item['pass'];
$this ->email = $item['mail'];
$this ->fechar = $item['fechar'];
$this ->ultima = $item['ultima'];
$this ->img_src = $item['img_src'];
$this ->reputacion = $this ->fechar = $item['reputacion'];
}
}
function miniatura(){
$html_mini = "<div>$this->usuario</div>";
return $html_mini;
}
pregunta.php (i'm only including relevant code for this problem)
Código PHP:
function get_autor(){
$us = new usuario($item['id']);
return $us->miniatura();
}
function imprimir_titular(){
$html_t = '<h1 class="prg'.$this->id.'" >[ '.$this->id_eval_q.' ] '.$this->get_autor().' pregunta: '.$this->pregunta.' , '.$this->fecha.'</h1>';
return $html_t;
}
And this is the error:
Cita:
Fatal error: Call to undefined method
usuario::miniatura() in
/home/piscolab/public_html/keepyourlinks.com/recetorium/clases/pregunta.php
on line 35 No entiendo por qué no
accede al método de la clase usuarios,
aunque me deje crear el objeto usuario
:S
Details:
- Protected atributes
Any help will be wellcome
I copied you code, change content of methods and everything works
class usuario {
function __construct($id){
echo 'ok';
}
function miniatura(){
echo 'ok';
}
}
function get_autor(){
$us = new usuario($item['id']);
return $us->miniatura();
}
Show full classes because with Your code as shown in nothing wrong.
ok, this is the file where i'm calling both:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Recetorium > Preguntas - Pregunta o responde qué y cómo cocinar algo
Cargando..
and in router.php
<?php require_once('funciones.php');
if(isset($_POST['inicio'])){
// el usuario está iniciando sesion
$iniciando = new sesion_usuarios();
if($iniciando->iniciar()){
imprimir_sesion_iniciada();
}else{
imprimir_formulario_sesion();
}
}else if(isset($_POST['registro'])){
$registrando = new registro_usuarios();
if($registrando->register()){
imprimir_usuario_registrado();
}else{
imprimir_formulario_registro();
}
}else if(isset($_GET['que']) or isset($que)){
if(isset($que))
$tarea = $que;
else
$tarea = $_GET['que'];
if($tarea == 'registro'){
imprimir_formulario_registro();
}else if($tarea == 'login'){
imprimir_formulario_sesion();
}else if($tarea == 'salir'){
cerrar_sesion();
}else if($tarea == 'ultimas_preguntas'){
listar_preguntas();
}else if($tarea == 'nueva_pregunta'){
$tem = new pregunta();
$tem->imprimir_formulario;
}else if($tarea == 'ultimas_recetas'){
$tem = new pregunta();
$tem->imprimir_formulario;
}
}else if(sesion()){
echo 'Pronto prodrás: Preguntar cosas, responder cosas y evaluar ambos. Publicar tus recetas, descubrir otras, evaluarlas y ser evaluado.';
}else{
$archivo = 'bienvenida.php';
include($archivo);
imprimir_formulario_sesion();
}
?>

Categories