Jump to content

Recommended Posts

Posted

Estou aqui a tentar replicar um sistema de notificação, mas estou com um problema.

Tenho uma sistema de notificação onde apenas funciona metade.

Nos seguintes ficheiros 

public static function likePost($postId, $likerId) {
			if (!DB::query('SELECT user_id FROM post_likes WHERE post_id = :postid AND user_id = :userid', array(':postid' => $postId, ':userid' => $likerId))) {
				// Aqui é chamada a função createNotify que vai criar a notificação
				Notify::createNotify("", $postId);
			}
		}

 

a função create Notify é a seguinte

class Notify {
		public static function createNotify($text = "") {
			$text = explode(" ", $text);
			$notify = array();
			
			foreach ($text as $word) {
				if (substr($word, 0, 1) == "@") {
					$notify[substr($word, 1)] = array("type" => 1, "extra" => ' { "postbody": "' .  htmlentities(implode($text, " ")) . '" } ');
				}	
			}

			if (count($text) == 0 && $postid != 0) {
				$temp = DB::query('SELECT posts.user_id AS receiver, post_likes.user_id AS sender FROM posts, post_likes WHERE posts.id = post_likes.post_id AND posts.id = :postid', array(':postid' => $postid));
				$r = $temp[0]["receiver"];
				$s = $temp[0]["sender"];
				DB::query('INSERT INTO notifications VALUES (:id, :type, :receiver, :sender, :extra)', array(':id' => NULL, ':type' => 2, ':receiver' => $r, ':sender' => $s, ':extra' => ""));
			}
			return $notify;
		}
	}

Esta função está a fazer duas coisas diferentes.

Cria e insere um notificação na tabela apropriada sempre que alguém refere o nome, por exemplo @sEnte, Aqui sei que alguém mencionou o meu nome num post qualquer.

A segunda parte, sempre que alguém faz like a um post qualquer meu eu deveria receber um notificação a dizer que alguém gostou. 

A diferença entre os dois INSERT INTO notifications é que no @ o $r vai buscar ao post e o $s vai buscar ao login actual.

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Posted

Esse id ser NULL não me cheira... Se é chave e é controlada pela base de dados, remove o campo do insert.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Posted (edited)

eu desconfio que o problema não está na query ... deve estar aqui:

$text = explode(" ", $text);
// ...
if (   count($text) == 0 // ou esta verificação
    && $postid != 0) {    // ou esta verificação, isto porque $postid caiu do ceu ...
  // ...
}
Edited by HappyHippyHippo
added $ to postid
IRC : sim, é algo que ainda existe >> #p@p
Posted

o link é qualquer coisa do tipo notify.php?postid=x

no início da função onde tem o text ="" mesmo que meta $postid = 0 e nesse SELECT meta ':postid' => $_GET['postid'] isto não me vai buscar o id do post....

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Posted

O único sítio onde estou agora a ver que possa dar problema é aqui

WHERE posts.id = post_likes.post_id AND posts.id = :postid'

E resto até pode estar debaixo do meus olho mas não o estou a ver....

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Posted

mas tens a certeza que o query de selecção é executado ? faz um output qualquer após o query para ver se ele é realmente executado assim como que dados ele retorna.

se for executado e não retornar dados nenhuns, o problema está na existência dessas relações na BD

IRC : sim, é algo que ainda existe >> #p@p
Posted

Não sei se foi a melhor maneira mas no if if ( count($text) == 0 dei valor  em vez de 0 e já funciona. Não sei é se estraguei alguma coisa mas agira também não posso experimentar. Vejo mais tarde.

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.